Parse JSON with org.json [duplicate]
Asked Answered
M

3

23

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?

Malloy answered 22/6, 2011 at 15:22 Comment(2)
After parsing the data from the JSON input, how do you intend to use it in Java code? Are you trying to populate a custom Java data structure, or do you just want to quickly retrieve one or two values from the JSON and move on?Foot
This short video that demonstrates reading and parsing JSON using the org.json library.Influenza
L
40

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

Loftus answered 22/6, 2011 at 15:34 Comment(6)
With that I get the following error on the last line: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type: <any>Malloy
Can you post the code you are having problems with?Loftus
With the exact code you posted, the only difference is the url usedMalloy
the above code may throw an exception, are you catching or handling exceptions appropriately?Loftus
Broken link above: json.org/javadoc/index.html?org/json/package-summary.htmlRegurgitation
fixed the broken linkLoftus
W
10

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.

Writeoff answered 13/7, 2011 at 7:45 Comment(0)
H
-1

I'd pass it

map<String, Object> 

loaded with

map<String, Object> 

in the object field.

Basically recreating the hierarchy of your java classes inside of a large map.

Example :

return ( Map<"GetFolderFilesByZoneResult", Map<"Result1", (object by id 98748) | "Result2", (object by id 99107) | "Result3", etc.

JSON will return that big map very pretty like, and programmatically it's easier to do then lists.

Holdup answered 22/6, 2011 at 15:32 Comment(3)
I see, but how can I parse that objects/arrays/values with org.json? Can you provide some code?Malloy
It does it automagically. Push it an object and it decodes it. For instance in my map example, it decodes the objects variable "String ID = 99107" field into JSON as "ID : 99107".Holdup
I think the question was exactly the opposite, take a json content and get it's value in java object.Housekeeper

© 2022 - 2024 — McMap. All rights reserved.