This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 7 years ago.
I am trying to parse JSON data which is like:
{
"name1": "xyz",
"data":[{
"education": {
"School": "xyz",
"ug": "xyz",
"Activities": [{
...
}],
"Prizes": [{
...
}],
"Curriculum":[{
...
}]
}
}]
}
How can I fetch the JSONArray of activities, prizes, curriculum values?
If you just want the values of the first element (you are looking at an array so you may want all the elements) you can try this:
String src = " { ... } "; //your json
JSONObject mainObject= new JSONObject(src);
JSONArray dataArray= mainObject.getJSONArray("data");
JSONObject firstDataObject = dataArray.getJSONObject(0); //get the first element
JSONObject educationObject = firstDataObject.getJSONObject("education");
JSONArray activitiesArray = educationObject.getJSONArray("Activities");
//do something with the array. Ex: activitiesArray.getJSONObject(0);
JSONArray prizesArray = educationObject.getJSONArray("Prizes");
//do something with the array
JSONArray curriculumArray = educationObject.getJSONArray("Curriculum");
//do something with the array
Related
I would like to convert the response.getBody of above call to array.
I am trying to parse only the array "data" of json as list.
JSON:
{
"totalValue": 21,
"data": [
{
"id": 1,
"firstname": "Tom",
"lastname":"Pit"
},
{
"id": 2,
"firstname": "Jim",
"lastname":"Sol"
}
]
}
So after some tries i reach here:
JSONParser parser = new JSONParser();
Object obj = (Object) parser.parse(response.getBody());
JSONArray array = new JSONArray();
array.add(obj);
This array has size: 1 in the array there is a json object with 2 values first is long value of the total value (21) second is JsonArray with value : all the values and key "data" .
I would like to parse the JsonArray as list of object in java...but whatever to try get the error most of the times.....
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column
Any help?
If you want to parse the "data" as list of object in java you can try with:
Map<?, ?> mapResponse = response.getBody();
List<?> data = (List<?>)mapResponse.get("data");
I hope this help you.
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 4 years ago.
i want to parse this code to get the id's location in an array.
Json is as follows:
"tipSecurableSet": {
"id": "082bdd09-6cff-41f9-a6f6-16a5bcc2eaa6",
"items": [
{
"typeId": "c5831702-15ed-483d-8253-c890e3f97dae",
"ids": [
"44b3efd4-5f7f-4698-aba4-1f4d9605c4eb"
],
"id": "b838aa0d-522a-411e-958a-41c711c6a856"
},
{
"typeId": "01c3c7de-2856-4665-90bc-a614caf335cd",
"ids": [
"8240a190-7e14-4ba4-87be-22febd09fa69"
],
"id": "62e62bf5-7a18-4518-a917-bb908d61fdd2"
}
]
}
}
You should take a look at Org.JSON library.
You can get the IDs very easily once you have your JSONObject. There are many ways to construct a JSONObject. You can parse XML.toJSONObject or use one of the JSONObject constructors.
JSONObject obj= new JSONObject(...);
Iterator<String> keys= obj.keys(); //Gets your Fields or Keys
To get the object for a key, here is an example with a String and an Array of JSONObjects.
String id= jsonArray.getJSONObject(i).getString("typeID")
This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 7 years ago.
I have the following JSON text and would like to get the values "detest" and "dislike".
{
"noun": {
"syn": [
"hatred",
"emotion"
],
"ant": [
"love"
]
},
"verb": {
"syn": [
"detest",
"dislike"
],
"ant": [
"love"
]
}
}
I've tried parsing it using the org.json library with the following:
JSONArray obj = new JSONObject(String);
JSONArray arr = obj.getJSONArray("syn");
But I can't seem to access the array this way.
Any help is greatly appreciated.
The following is related to the JSON.simple library.
You need a JSONParser instance to get a JSONObject value from your String object.
JSONObject data;
try {
JSONParser helper = new JSONParser();
data = (JSONObject)helper.parse(String);
} catch (ParseException parse) {
// Invalid syntax
return;
}
// Note that these may throw several exceptions
JSONObject node = (JSONObject)data.get("verb");
JSONArray array = (JSONArray)node.get("syn");
This question already has answers here:
How to parse JSON file?
(3 answers)
Closed 8 years ago.
"papers": [
{
"files": [
{
"url": "http://farnsworth.papro.org.uk/file/977",
"type": "Initial XML version of the paper",
"name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8.xml"
},
{
"url": "http://farnsworth.papro.org.uk/file/978",
"type": "Final annotated XML file",
"name": "c6938201-dac0-4ef9-91cd-ca6e8c30f4b8_final.xml"
}
]
How can i get the first url in this json file ? What I have so far is:
JSONObject file = (JSONObject) files.get(j);
String url = (String) file.get("url");
System.out.println(url);
something like this should work :
JSONArray results = d.getJSONArray("files");
JSONObject p = (JSONObject)results.get(0);
url = p.getString("url");
This question already has answers here:
Converting JSON data to Java object
(14 answers)
Closed 8 years ago.
{
"users":[ {
"user":"hi","password":"hi"
}, {
"user":"test","password":"test"
}
]
}
How to parse this type of JSON Objects?
Please help..
You need to use a json library like gson, jsonlib or jackson.
JSONObject: it is a hash object like Map where key value pairs are used
JSONArray: It is a collection of objects like List
JSONObject works like a map with key-value pairs. Eg. Code looks like below :
JSONObject obj=new JSONObject();
obj.put("name","Hello");
obj.put("nickname","Hi");
StringWriter out = new StringWriter();
obj.writeJSONString(out);
String jsonText = out.toString();
System.out.print(jsonText);
JSONArray works like a list , Eg, code below:
JSONArray list = new JSONArray();
list.add("Hello");
list.add(new Integer(100));
System.out.print(list);
You can differentiate the JSONArray & JSONObject as below:
JSONArray
A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values.
[ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
JSONObject
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names.
{"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
You can parse the JSONObject as below:
JSONObject JsonObject = new JSONObject(json);
JSONArray JsonArray_ = JsonObject .getJSONArray("users");
for (int i = 0; i < numberOfItems; i++) {
JSONObject record= JsonArray_photo.getJSONObject(i);
parsedObject.user = record.getString("user"); //its the same for all fields
parsedObject.password = record.getString("password");
map.add(parsedObject);
}