1

I was just wondering if anyone could recommend a better alternative method than org.json for decoding a complex JSON string. For reference, this will be coming from a web server down to Android (& iOS, but that's the other dev's problem!) devices, it doesn't have to go back up.

The string is of the following nature...

{"header":"value","count":value, ..., "messages":[
    {"messagetype":1,"name":"value"},
    {"messagetype":2,"name":"value","name":value},
    {"messagetype":1,"name":"value"},
    {"messagetype":3,"name":"value","subvalues":["value",value,value]},
    ...
    {"messagetype":4,"name":value,"name":"value","name":value}
]}

Basically, there are some header fields which I can always rely on but then there will be an "array" of messages, variable in count, content and order.

I've been researching this for a few days now and have dismissed GSON and a few others because that either need to know the exact structure in advance and/or don't deal well with the embedded types (the contained messages).

Answer three in this question pointed me to using the org.json library and I know I can use that to parse through the string but I guess one of that answer's replies ("That's super old school and nobody uses that library anymore in the real world") has made me question my approach.

Can anyone suggest a library/approach which would handle this problem better? If anyone else has used an alternative approach to dealing with this type of complex and variable structure, I'd really appreciate your input.

Thanks in advance.

Community
  • 1
  • 1
Mike
  • 2,120
  • 1
  • 23
  • 40
  • Are those typos on your message lines? `{"messagetype":2,"name":"value","name":value}` is not valid JSON (cannot have duplicate keys, cannot have non-delimited string literals). Actually, your whole JSON example is littered with invalid syntax. – Perception May 18 '13 at 05:17
  • The example is an approximation to convey the structure, not an exact example of the data in question, hence "of the following nature" – Mike May 18 '13 at 05:39
  • 1
    Android comes with three embedded JSON libraries. I would recommend looking at [Gson](https://code.google.com/p/google-gson/) again, as not only does it do what you think it doesn't, but it also provides many more features that you will eventually need (though you dont think you do now). For manual parsing, start with the [JsonParser](http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html?com/google/gson/JsonParser.html) class. – Perception May 18 '13 at 06:07
  • Thanks for the pointer to the JsonParser, I'll have a look. Unfortunately all the gson examples I found were based on knowing the exact structure of the source data and some even went on to say it couldn't handle the more complex types, none mentioned the Parser. I already have code based on org.json written so will probably continue that way for now but thanks for the heads-up. – Mike May 18 '13 at 06:12
  • No worries and good luck. If you ever do decide to go with Gson you can see an example of using the JsonParser [here](http://stackoverflow.com/a/16596863/680925), and a more complex example [here](http://stackoverflow.com/a/16578616/680925). – Perception May 18 '13 at 06:25

1 Answers1

1

I really do not agree with the opinion about org.json libray: "That's super old school and nobody uses that library anymore in the real world", since parsing json by using this library is pretty straightforward. Besides, how complex can json get?, I mean, is all about key/value pairs, nothing that can't be solved with a few lines of code, for instance I will illustrate you a few cases, so that you'll get convinced that is pretty simple to do:
Suppose you have a response from the server containing all info you need formatted in a json array, then you can do something like this to parse the String:

JsonArray arrayJson = new JsonArray(response); 

But now you want to access arrayJson childs:

for (int i = 0; i < arrayJson.length() - 1; i++)
{
  JsonObject json = arrayJson.getJSONObject(i);
}

And now assume you have another array of json's inside those you retrieved in the for loop: Then you'll get them this way:

for (int i = 0; i < arrayJson.length() - 1; i++)
{
  JsonObject json = arrayJson.getJSONObject(i);
  JSONArray anotherArray = json.getJSONArray("key");
}

....., more nestings you can handle them the same way, so I think I established my point. Remember that sometimes, struggling on finding easier ways to do things, can get them even harder to do.

Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
  • Thanks for your comment, it supports what I was originally thinking when I got sidetracked by the old school comment I saw. I totally agree with what you say and, frankly, think it is a more flexible and elegant solution because of its simplicity. – Mike May 18 '13 at 05:47
  • Happy to accept your answer as it coincides with my own original thinking and, I believe, will give me the most flexible solution. In many ways I prefer writing my own parsing solution and it will be interesting to compare the org.json parsing with the gson further down the track. – Mike May 18 '13 at 07:15