3

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.

2
  • ... "Show me what can solve this problem." Wow, very commanding. What have you tried so far? Commented Nov 8, 2012 at 2:53
  • Additionally, is that the entirety of LogObject? If so, setting any values in it will be rather hard... Commented Nov 8, 2012 at 3:17

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

I think getQueryParameterNames() might be in Android but not in Java.
You only need to prepend "?" to the query string to get it parsing correctly.
2

Etiquette

  1. You really should be much more specific about what you have tried and why it didn't work.

  2. A proper code sample of your LogObject would really be very helpful here.

  3. 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.

4 Comments

ok, that look very good. So URL query string always in utf-8 encode. more thing?
So 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); } }
Yeah, sure. If you're okay with throwing a runtime exception and (presumably) crashing your program, then that should be OK.
Every implementation of Java is required to support UTF-8 and the encoding is given as a constant, so a runtime exception is correct here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.