Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON with org.json [duplicate]

I'm trying to parse an output from a server that looks like this:

{
  "GetFolderFilesByZoneResult": [
    {
      "ID": 98748,
      "CreatedBy": "",
      "UpdatedBy": "none",
      "CreatedDate": "\/Date(1308273033620+0100)\/",
      "UpdatedDate": "\/Date(1308303003770+0100)\/",  
      "CommentCount": 0,
      "Key": "",
      "Enabled": true,
      "MimeType": "video",
      "Votes": 2,
      "TotalRating": 0,
      "AllowComments": true,
      "ViewCount": 323,
      "ReleaseDate": "\/Date(1308273000000+0100)\/",
      "ExpireDate": "\/Date(4102444800000+0000)\/",
      "Author": "",
      "Size": 133799936,
      "Tag1": "",
      "Tag2": "",
      "Tag3": "",
      "RecycleBin": false
    },
    {
      "ID": 99107,
      "CreatedBy": "",
      "UpdatedBy": "none",
      "CreatedDate": "\/Date(1308583412520+0100)\/",
      "UpdatedDate": "\/Date(1308583564007+0100)\/",     
      "CommentCount": 0,
      "Key": "",
      "Enabled": true,
      "MimeType": "video",
      "Votes": 0,
      "TotalRating": 0,
      "AllowComments": true,
      "ViewCount": 33,
      "ReleaseDate": "\/Date(1308583380000+0100)\/",
      "ExpireDate": "\/Date(4102444800000+0000)\/",
      "Author": "",
      "Size": 47955968,
      "Tag1": "",
      "Tag2": "",
      "Tag3": "",
      "RecycleBin": false
    }
  ]
}

I'm trying to use Java org.json to parse it, but I don't have any experience with JSON/org.json, so I'm having a little trouble. How can I parse this?

like image 697
Paul Ulrich Avatar asked Jun 22 '11 15:06

Paul Ulrich


People also ask

Does JSON object allow duplicate keys?

We can have duplicate keys in a JSON object, and it would still be valid. The validity of duplicate keys in JSON is an exception and not a rule, so this becomes a problem when it comes to actual implementations.

Does output type application JSON support duplicate key?

The short answer: Yes but is not recommended.

What happens if you JSON parse a JSON object?

parse() raises an error, stops processing, and returns no processed data, even if the rest of the JSON is correct. If an error occurs, never assume that JSON. parse() returns a specific value. Depending on how you write your program, an error could stop the rest of your Javascript code from executing.

What does JSON parse () method do?

The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


2 Answers

1) Assuming you have the JSON libraries on your path (from www.json.org), it's pretty easy.

import org.json.JSONTokener;
...

URI uri = new URI("http://someserver/data.json");
JSONTokener tokener = new JSONTokener(uri.toURL().openStream());
JSONObject root = new JSONObject(tokener);

From there, you can address the various parts of the JSON object. Take a look at the Javadocs for the specifics. https://developer.android.com/reference/org/json/package-summary.html

like image 191
JohnnyO Avatar answered Sep 25 '22 03:09

JohnnyO


Here is the most universal solution, which allows to parse any JSON type into appropriate Java type:

Object json = new JSONTokener(response).nextValue();

Then you can determine resulting type and handle it appropriately.

like image 27
Nevkontakte Avatar answered Sep 22 '22 03:09

Nevkontakte