Here is the structure of wanted JSON string
"1" : {
"Class': "group"
"text": "test1"
}
"2" : {
"Class': "group"
"text": "test1"
}
How can I create an jsonObject in Java that has this JSON representation?
You can either directly create the structure with Strings, or use a library that will create JSON objects in a more object oriented manner.
See https://stackoverflow.com/questions/338586/a-better-java-json-library for some discussion on various JSON libraries and their pros/cons.
You can also always go with JSON's official library: http://json.org/java/
Use JSONObject Class from net.sf.json.JSONObject like this:
JSONObject jsonObject=new JSONObject();
JSONObject childObject=new JSONObject();
childObject.put("class", "group");
childObject.put("text", "test1");
jsonObject.put("1", childObject);
jsonObject.put("2", childObject);
System.out.println(jsonObject.toString());
Related
I'm new to JSON and have below file. I have to save the array "steps" in java and need to loop the objects "duration" , "status" and "Keyword".
"steps": [
{
"result": {
"duration": 7128811788,
"status": "passed"
},
"line": 5,
"name": "The Browser is Launched and Smart Business URL is loaded",
"match": {
"location": "Common_Login.the_Browser_is_Launched_and_Smart_Business_URL_is_loaded()"
},
"keyword": "Given "
},]
I tried below but didn't worked.
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("./target/JSON/Output.json"));
JSONArray jsonArray = (JSONArray) obj;
System.out.println(jsonArray);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObjectRow = (JSONObject) jsonArray.get(i);
name = (String) jsonObjectRow.get("duration");
id = (String) jsonObjectRow.get("status");
uri = (String) jsonObjectRow.get("name");
status = (String) jsonObjectRow.get("location");
}
Refer here.
There are so many libraries build in for doing this task. But look at the question above.
If you want to use the built-in class from Java you can have a look here:
https://stackoverflow.com/a/17399110/3977134
You are missing the part from the parseJson function in the referenced answer.
For simplicity I'd suggest to you to use org.json which is just one of many good libraries to use for JSON parsing in Java. If you are interested see here:
Parse JSON with org.json
Parsing JSON in Java Using org.json
I am new To JSon and i want to search the following json string and get the required output.
String:
{"status":"Success","code":"200","message":"Retrieved Successfully","reason":null,"
"projects":
[
{
"projectName": "example",
"users":
[
{
"userName": "xyz",
"executions":
[
{
"status": "check",
"runs":
[
{
"Id": "------",
"Key": "---"
}
],
"RCount": 1
}
],
"RCount": 1
}
],
"RCount": 1
},
Like that i have many projects and now , if i give projectname and username as input i wantt to get its status as output.
Is it possible?If yes how?
You may use JSONObject for this.
JSONObject json = new JSONObject(string);
JSONArray[] projectsArray = json.getJSONArray("projects");
for(int i = 0; i < projectsArray.length; ++i)
{
String projectName = projectsArray[i].getString("projectName");
...
}
Use the same method to get the users.
You can use gson library. Using gson convert your json string to Map and then you can iterate through map to get required item
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(jsonString, type);
You can use the Google gson to map your json data structure to a Java POJOs.
Example :
You can have Projects class containing list/array of Users.
Users class containing list/array of Executions and so on.
Gson library can easily map the json to these classes as objects and you can access your data in a more elegant manner.
Here are a few references :
http://howtodoinjava.com/2014/06/17/google-gson-tutorial-convert-java-object-to-from-json/
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
I am a newbie in Java, i'm researching how to parse json to object in java.
I have the following Json content:
{
"objects": [
{
"type": "image",
"left":0,
"top":0,
"width":787,
"height":1165,
"src":"image/16_011020002_000_bk.PNG",
"replaceable":false,
"lockObject":false
},
{
"type": "image",
"left":70,
"top":54,
"width":669,
"height":469,
"src":"image/16_011020002_000_il.PNG",
"replaceable":false,
"lockObject":false
},
{
"left":70,
"top":54,
"width":669,
"height":469,
"direction":"v",
"fontFamily":"KaitiEG4-Medium-SJIS",
"fill":"#55626C",
"text":"旧年中は大変お世話になり\nありがとうございました\n本年も相変わらずご支援のほど\nお願い申し上げます\n\n 平成二十八年 元旦",
"textAlign":"left",
"lockObject":false
},
{
"left":70,
"top":54,
"width":669,
"height":469,
"direction":"v",
"fontFamily":"LeisuEG4-Medium-SJIS",
"fill":"#55626C",
"text":"謹んで\n 初春のお慶びを\n 申し上げます",
"textAlign":"left",
"lockObject":false
}
]
}
How to design an object for this json and how to parse json to that object?
Help me this issue. Thank you!
Use some kind of JSON parser.
GSON
https://github.com/google/gson
or Jackson
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
I hope it will help you...!
use Jackson-
JSONArray objects=new JSONObject(jsondata).getJSONArray("objects");
for(int i=0;i<objects.length();i++){
JSONObject object=objects.getJSONObject(i);
System.out.println("value of left=="+object.getString("left"));
System.out.println("value of top=="+object.getString("top"));
}
As per your question its seems to be an array representing the json data of type object.
To parse the data we can use the above two parsers mentioned by d__k.
I have been using Jackson and we have a ObjectMapper class which converts the data in the specified type. We can use ObjectMapper#readValue to read the data into java object.
Kindly find more information at this link.
I would like to walk through the json file below and save the part after "coordinates".
The json file:
{"type": "FeatureCollection","features": [{"type": "Feature","properties": {},"geometry": {"type": "LineString","coordinates": [[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]
]
}
}
]
}
I've seen code using getJSONArray() and getJSONObject() here and here. This information helped me to select (in my case) the "geometry" tree.
My code so far (test2.geojson is the json file mentioned above):
String output = new String(Files.readAllBytes(Paths
.get("C:\\Users\\****\\Desktop\\test2.geojson")));
JSONObject obj = new JSONObject(output);
JSONArray jsonArray = obj.getJSONArray("features");
System.out.println(jsonArray);
However, this only rearranges the file and appends the first part to the end of the file.
[{"geometry":{"coordinates":[[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]],"type":"LineString"},"type":"Feature","properties":{}}]
Any solutions to get the desired output of:
[[4.354282,52.032195],[4.354087,52.032462],[4.353783,52.032962],[4.353579,52.033437],[4.353333,52.034151],[4.352991,52.03545],[4.352517,52.037002],[4.352442,52.037352],[4.352368,52.0378],[4.352336,52.038238],[4.352331,52.039962],[4.352346,52.040706]
]
}
}
]
}
Thanks in advance.
Cheers!
As "geometry" is a json object {}, "coordinates" is a json array [],
You should be doing
JSONArray jsonArray = obj.getJSONArray("features");
JSONArray resultArray = jsonArray.getJSONObject[0].getJSONObject("geometry").getJSONArray("coordinates")
I have a json structure like this:
[{
"name": "test",
"urlImg": "test",
"description": "test",
"urlLink": "test",
}]
And I want to retrieve the 4 values, I tried with:
JSONObject jsonObj = new JSONObject(jsonStr);
name = jsonObj.getString("TAG_TITLE");
I correctly download the json but name doesn't retrieve anything.. Am I parsing it wrong?
Your JSON is array [...] with one object {..}. So you need to
create JSONArray
get its only JSONObject
read its properties