This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
How to parse this json response using Java
{
"Name": {
"name_description": "NIL",
"date": "NIL"
},
"Age": {},
"City": {},
"SOAP": [
["content", "subtopic", "topic", "code"],
["I advised her to call 911, which he did.", "history of present illness", "subjective", "{}"]
]
}
You'd have to use an external library like json-simple
Read more about it here
Use a library called org.json, it is honestly the best java json library.
for example:
import org.json.JSONObject;
private static void createJSON(boolean prettyPrint) {
JSONObject tomJsonObj = new JSONObject();
tomJsonObj.put("name", "Tom");
tomJsonObj.put("birthday", "1940-02-10");
tomJsonObj.put("age", 76);
tomJsonObj.put("married", false);
// Cannot set null directly
tomJsonObj.put("car", JSONObject.NULL);
tomJsonObj.put("favorite_foods", new String[] { "cookie", "fish", "chips" });
// {"id": 100001, "nationality", "American"}
JSONObject passportJsonObj = new JSONObject();
passportJsonObj.put("id", 100001);
passportJsonObj.put("nationality", "American");
// Value of a key is a JSONObject
tomJsonObj.put("passport", passportJsonObj);
if (prettyPrint) {
// With four indent spaces
System.out.println(tomJsonObj.toString(4));
} else {
System.out.println(tomJsonObj.toString());
}
}
Related
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 2 years ago.
Any idea how I can parse a Json like this into a java entity?
{
"-MR0myiEK5jDOdthWeMT": {
"birthday": "Date5",
"name": "Check 1"
},
"-MR0n-86JCqxuO7C2HfZ": {
"birthday": "Date3",
"name": "Check 2"
},
"-MR0n0VCXBw-32tfq738": {
"birthday": "Date1",
"name": "Check 4"
}
}
I am using spring and wanted to parse it into a java class like this:
class Person{
String name;
String birthday;
}
The org.json library is easy to use.
Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation
[ … ] represents an array, so library will parse it to JSONArray
{ … } represents an object, so library will parse it to JSONObject
Example code below:
import org.json.*;
String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");
JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
String post_id = arr.getJSONObject(i).getString("birthday");
......
}
I would use the
jackson
library that is already included in the spring boot dependencies.
This question already has answers here:
Pretty-Print JSON in Java
(20 answers)
Closed 5 years ago.
For example, I want to print it as below, instead of one single line. This is a JSON string. By default, myJsonObject.toString() is a one-line String. Is there some method from org.json.JSONObject that can directly output this formatted form?
{
"name":"John",
"age":30,
"cars": [
{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
{ "name":"Fiat", "models":[ "500", "Panda" ] }
]
}
There are different ways to print pretty json string.
GSON offers a method setPrettyPrinting(),
For instance,
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jsonElement = new JsonParser().parse(jsonString);
System.out.println(gson.toJson(jsonElement));
To indent any old JSON, just bind it as Object, like:
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(myJsonObject, Object.class);
and then write it out with indentation:
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
This question already has answers here:
How to parse JSON in Java
(36 answers)
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 6 years ago.
how to directly parse JSONdata using JSONObject in java as my JSON Data don't have any JSONArray .
JSONData:-
{{
"id": 481,
"date": "2016-12-30T13:56:10",
"date_gmt": "2016-12-30T13:56:10",
"guid": {
"rendered": "http://www.mytrendin.com/wp-content/uploads/2016/12/read- 1710011_1280.jpg"
},
"modified": "2016-12-30T13:56:20",
"modified_gmt": "2016-12-30T13:56:20",
"slug": "read-1710011_1280",
"type": "attachment",
"link": "http://www.mytrendin.com/increase-child-development/read-1710011_1280/",
"title": {
"rendered": "child development"
}}
java code
jsonObject = new JSONObject(results);
for(i=0;i<jsonObject.length();i++){
jsonObject=jsonObject.getJSONObject();
j = jsonObject.getString("type");
// mainActivityModel.setId();
}
You json is incorrect.
{
"id": 481,
"date": "2016-12-30T13:56:10",
"date_gmt": "2016-12-30T13:56:10",
"guid": { "rendered": "http://www.mytrendin.com/wp-content/uploads/2016/12/read- 1710011_1280.jpg" },
"modified": "2016-12-30T13:56:20", "modified_gmt": "2016-12-30T13:56:20",
"slug": "read-1710011_1280",
"type": "attachment",
"link": "http://www.mytrendin.com/increase-child-development/read-1710011_1280/", "title": { "rendered": "child development" }
}
You can parse json by this.
JSONObject obj = new JSONObject(result);
JSONObject guid=obj.getJSONObject("guid");
To get json object you will call getJSONObject() and to get String you will call getString()
And if you need to parse a json Array
JSONArray json_arr = new JSONArray(results);
for(i=0;i<json_arr.length();i++){
JSONObject jsonObject=json_arr.getJSONObject(i);
}
go through this tutorial it can help you in understanding json parsing
http://www.technotalkative.com/android-json-parsing/
This question already has answers here:
Parsing JSON Array within JSON Object
(5 answers)
Closed 6 years ago.
Apologies, I have tried multiple things here and seem to run into some issues. This should be simple.
JSON file :
{
"content": [
{
"media_type": "text/html",
"text": "<p>Hello world</p>"
},
{
"media_type": "text/plain",
"text": "Hello world"
}
],
"id": "123",
"title": "no-title"
}
I have a JSONObject created from this string.
I have tried -
String txtFromJSON = json.getJSONObject("content").getJSONObject("text").toString();
String txtFromJSON = json.getString("content.text");
String txtFromJSON = json.getString("content");
All of these fail.
The output I would like is simply the
<p>Hello world<p>
from the first text field.
Is there any simple way for me to get this data stored in a variable?
Thanks.
try this:
final JSONObject obj = new JSONObject(youJsonString);
final JSONObject content = obj.getJSONArray("content");
final int n = content.length();
if(n ==1 ){
String txtFromJSON = json.getString("text");
}
This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 9 years ago.
Iam new to java script and JSON, please help me in solving my problem. Below is the structure of my JSON in JavaScript
{
"name": "sample",
"def": [
{
"setId": 1,
"setDef": [
{
"name": "ABC",
"type": "STRING"
},
{
"name": "XYZ",
"type": "STRING"
}
]
},
{
"setId": 2,
"setDef": [
{
"name": "abc",
"type": "STRING"
},
{
"name": "xyz",
"type": "STRING"
}
]
}
]
}
in the backend, what should be the synatx of java method to receive this data
public void getJsonData(****){
}
How to parse this JSON data in java and what should be the syntax of method parameter ?
update 1: Edited the json format to make it valid
First create a class that will map your json object and give a name something like "DataObject". Then use the gson library and do the following:
String s = "";
DataObject obj = gson.fromJson(s, DataObject.class);
Your JSON is invalid, but assuming you fix that then you are looking for a library in Java which will serialize an annotated Java class to JSON, or deserialize JSON data to an annotated Java class.
There is a whole list of suitable libraries here:
http://json.org/java/