Parse a query string parameter to java object
Solution 1
Etiquette
You really should be much more specific about what you have tried and why it didn't work.
A proper code sample of your
LogObject
would really be very helpful here.Ideally, you would provide a SSCCE so others could easily test your problem themselves.
Answer
You can extract the name:value pairs like this:
String toParse = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056";
String[] fields = toParse.split("&");
String[] kv;
HashMap<String, String> things = new HashMap<String, String>();
for (int i = 0; i < fields.length; ++i)
{
t = fields[i].split("=");
if (2 == kv.length)
{
things.put(kv[0], kv[1]);
}
}
I have chosen to put them into a HashMap, but you could just as easily look at the name part (kv[0]
) and choose to do something with it. For example:
if kv[0].equals("ObjectGUId")
{
logObject.setGUId(kv[1]); // example mutator/setter method
}
else if //...
However, all your fields in LogObject
are private and you haven't shown us any methods, so I hope you have some way of setting them from outside... bear in mind you will need to store the pairs in a data structure of some kind (as I have done with a HashMap
) if you intend to intialise a LogObject
with all the fields rather than setting the fields after a constructor call.
Speaking of SSCCEs, I made one for this answer.
Solution 2
If you do not really need to push the querystring into your own class (you might want that though), instead of parsing it manually, you could use the URLDecoder, as @Sonrobby has commented:
String qString = "ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife";
Uri uri = Uri.parse(URLDecoder.decode("http://dummy/?" + qString, "UTF-8"));
if (uri != null) {
for(String key: uri.getQueryParameterNames()) {
System.out.println("key=[" + key + "], value=[" + uri.getQueryParameter(key) + "]");
}
}
The "dummy" looks dirty but it is required if what you only have is the querystring values (qString). If you have the complete URL, just pass it directly to the URLDecoder, and you are done.
Solution 3
Inspired by @bruno.braga, here's a way using Apache http-components. You leverage all the parsing corner cases:
List<NameValuePair> params =
URLEncodedUtils.parse("http://example.com/?" + queryString, Charset.forName("UTF-8"));
That'll give you a List of NameValuePair objects that should be easy to work with.
Comments
-
Sonrobby almost 2 years
I have query string like that:
ObjectGUId=1abcde&ObjectType=2&ObjectTitle=maximumoflife&Content=racroi&TimeStamp=2012-11-05T17:20:06.056
And I have Java Object:
LogObject{ private String ObjectGUId; private String ObjectType; private String ObjectTitle; private String Content; private String TimeStamp; }
So i want to parse this query string to this java Object.
I've searched and read many question but not gotten correct answer yet.
Show me what can solve this problem.
-
Sonrobby over 11 yearsok, that look very good. So URL query string always in utf-8 encode. more thing?
-
Sonrobby over 11 yearsSo URL query string always in utf-8 encode. So maybe use:
private static String urlDecode (String s) { try { return URLDecoder.decode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Error in urlDecode.", e); } }
-
Iskar Jarak over 11 yearsYeah, sure. If you're okay with throwing a runtime exception and (presumably) crashing your program, then that should be OK.
-
fgb over 11 yearsEvery implementation of Java is required to support UTF-8 and the encoding is given as a constant, so a runtime exception is correct here.
-
jokeyrhyme over 9 yearsI think
getQueryParameterNames()
might be in Android but not in Java. -
Kong almost 9 yearsYou only need to prepend "?" to the query string to get it parsing correctly.