This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
How do i parse a JSON that is like this?
[{
"id" : 28010942,
"type" : "trafficlight",
"title" : "225 - Shaw Blvd. / Gomezville - Flashing",
"activeFrom" : "Apr 30, 2013 6:18:00 PM",
"publiclyVisible" : true,
"locationLat" : 14.589366803498653,
"locationLon" : 121.03713870048522,
"publicDescription" : ""
}, {
"id" : 28010939,
"type" : "trafficlight",
"title" : "301 - Andalucia (A. Mendoza) / P. Campa - No Display",
"activeFrom" : "Apr 30, 2013 6:00:00 PM",
"publiclyVisible" : true,
"locationLat" : 14.608034456366056,
"locationLon" : 120.98599433898926,
"publicDescription" : ""
} ...
]
I can parse ones that have an object and jsonArrays but this one has neither. How can i iterate through these and be able to store each information like the "id". I'm using the org.json library, or should i use a different one?
If you need to parse try the below
[ // json array node
{ // json object node
"id": 28010942,
"type": "trafficlight",
To parse
JSONArray jarray = new JSONArray("Myjsonstring");
for(int i=0 ;i<jarray.length();i++)
{
JSONObject jb = (JSONObject)jarray.get(i);
String id = jb.getString("id");
String type = jb.getString("type");
// same for others title, activeform..
}
Related
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 1 year ago.
{
"courseId": 23,
"courseName": "science",
"courseCode": "SC100",
"courseDescription": "linear algebra",
"courseDuration": "6 m",
"createdDate": 1630438611000,
"updatedDate": null,
"removeImages": []
}{
"timestamp": 1630614081354,
"status": 200,
"error": "OK",
"path": "/api/editcourse/23"
}
I would use Jackson Project for that task.
Alternative 1
Since you have 2 values in the same Json and you want only the first, you can use readTree(String) method from ObjectMapper class. It will skip the second value.
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonResponse);
System.out.println(jsonNode.toPrettyString());
Output:
{
"courseId" : 23,
"courseName" : "science",
"courseCode" : "SC100",
"courseDescription" : "linear algebra",
"courseDuration" : "6 m",
"createdDate" : 1630438611000,
"updatedDate" : null,
"removeImages" : [ ]
}
Alternative 2
Using method readValues(JsonParser, Class<T>) you can read all values an iterate selecting only the ones you need.
ObjectMapper mapper = new ObjectMapper();
Iterator<?> iterator = mapper.readValues(new JsonFactory().createParser(jsonResponse), Map.class);
Object object = iterator.next();
System.out.println(object);
Output:
{courseId=23, courseName=science, courseCode=SC100, courseDescription=linear algebra, courseDuration=6 m, createdDate=1630438611000, updatedDate=null, removeImages=[]}
I have an String called inputJson that contains
{"listPruebas": [
{
"nombrePrueba" : "pruebaA",
"id" : 1,
"tipoPrueba" : "PRUEBABASE1",
"elementoBase" : "tipoA",
"listaMarca": [
{
"elemento": "elemento1 ",
"tipo": "ABC",
"cadena": "SFSG34235WF32"
},
{
"elemento":"elemento2",
"tipo":"DEF",
"cadena":"DJRT64353GSDG"
},
{
"elemento" : "elemento3",
"formato ":"JPG"
}
]},
{
"nombrePrueba" : "pruebaB",
"id" : 2,
"tipoPrueba" : "PRUEBABASE2",
"elementoBase" : "imagenPrueba",
"listaMarca2": [
{
"elemento" : "imagen",
"tipo": "tipo5",
"cadena": "iVBORw0KGgoAAAANSUhEUgAAAgAAAA"
}
]
}
],
"listaBuscar":
[
{
"tipoBusqueda":"busqueda1",
"id" : 1,
"operacion" : "operacion1",
"valor" : "12"
},
{
"tipoBusqueda":"binario",
"id" : 2,
"operacion" : "operacion2",
"valor" : "13"
},
{
"tipoFiltro":"numerico",
"id" : 31,
"operacion" : "MENOR_QUE",
"valor" : "1980",
"intervalo" : 1
}
]
}
and I converted the String to JSONObject of this way
JSONObject object = new JSONObject(inputJson);
and I got this
jsonObject::{"listaBuscar":[{"valor":"12","id":1,"operacion":"operacion1","tipoBusqueda":"busqueda1"},{"valor":"13","id":2,"operacion":"operacion2","tipoBusqueda":"binario"},{"tipoFiltro":"numerico","intervalo":1,"valor":"1980","id":31,"operacion":"MENOR_QUE"}],"listPruebas":[{"listaMarca":[{"tipo":"ABC","elemento":"elemento1","cadena":"SFSG34235WF32"},{"tipo":"DEF","elemento":"elemento2","cadena":"DJRT64353GSDG"},{"elemento":"elemento3","formato":"JPG"}],"elementoBase":"tipoA","tipoPrueba":"PRUEBABASE1","nombrePrueba":"pruebaA","id":1},{"elementoBase":"imagenPrueba","tipoPrueba":"PRUEBABASE2","listaMarca2":[{"tipo":"tipo5","elemento":"imagen","cadena":"iVBORw0KGgoAAAANSUhEUgAAAgAAAA"}],"nombrePrueba":"pruebaB","id":2}]}
and now I need to extract information but I dont know how to do, for example I try this
object.getString("elemento1");
but I got this error
Caused by: org.json.JONException: JSONObject["elemento1"] not found
help me please
You can't get a nest JSON object from the top level. It's like a treemap. You need to convert it into a java object or get it level by level.
check this post, a lot of ways.
You json contains two json arrays, fetch them as -
JSONArray listaBuscArray = jsonObj.getJSONArray("listaBuscar");
JSONArray listPruebasArray = jsonObj.getJSONArray("listPruebas");
Now you can process and use them as -
for(int i=0; i<listaBuscArray.length; i++){
JSONObject obj = listaBuscArray.getJSONObject(i);
.... your logic
}
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:
How to parse JSON in Java
(36 answers)
Closed 5 years ago.
I want to read Json file. My Json File has Content as follow
[
{
"arguments" : [
{
"IsEnabled" : "false",
"class" : "UITextField",
"width" : 238,
"parent" : {
"class" : "UIView",
"height" : 101,
"Y" : 192,
"width" : 280,
"X" : 20
},
"name" : "Enter UserName",
"X" : 40,
"isRightOf" : "NA",
"Recording Device" : "NA",
"Y" : 0
},
{
"data" : "Enter UserName",
"type" : "string"
}
],
}
]
I also tried GSON library to read JSON file.but fails . Please help
The JSON you provided is invalid (there is an invalid comma)
[
{
"arguments" : [
{
"IsEnabled" : "false",
"class" : "UITextField",
"width" : 238,
"parent" : {
"class" : "UIView",
"height" : 101,
"Y" : 192,
"width" : 280,
"X" : 20
},
"name" : "Enter UserName",
"X" : 40,
"isRightOf" : "NA",
"Recording Device" : "NA",
"Y" : 0
},
{
"data" : "Enter UserName",
"type" : "string"
}
], <-- this comma makes the json invalid
}
]
Your input json was wrong there is a comma missing as suggested.
Json objects are very hard to parse but if you once get the concept of how to parse the json data it is really easy.
You need to see if the property you are trying to access is a json array or an object. This is the basic rule if you are a beginner.
Here is the code::
OUTOUT IS::
arguments>>>>>>>>> [{"parent":{"width":280,"X":20,"Y":192,"class":"UIView","height":101},"Recording Device":"NA","IsEnabled":"false","width":238,"name":"Enter UserName","X":40,"isRightOf":"NA","Y":0,"class":"UITextField"},{"data":"Enter UserName","type":"string"}]
{"parent":{"width":280,"X":20,"Y":192,"class":"UIView","height":101},"Recording Device":"NA","IsEnabled":"false","width":238,"name":"Enter UserName","X":40,"isRightOf":"NA","Y":0,"class":"UITextField"}
{"data":"Enter UserName","type":"string"}
So, here in the code you can see that I have taken json array sometimes and object sometime, you need to differentiate between them.
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class test {
public static void main(String[] args) throws IOException, InterruptedException {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("test.json"));
JSONArray jsonObject = (JSONArray) obj;
JSONObject arr = (JSONObject) jsonObject.get(0);
JSONArray arguments = (JSONArray) arr.get("arguments");
System.out.println("arguments>>>>>>>>> "+arguments);
for(int i = 0 ; i< arguments.size() ;i++){
JSONObject object = (JSONObject) arguments.get(i);
System.out.println(object);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
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");