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.
Related
I have same query. My JSON is as below.
String json="{ "credentials": { "password": "Password"123", "emailAddress": "skylineadmin#gmail.com" }, "clientTimeMs": 1582006455421, "callerRole": 0 }"
key = password and value is "Password"123" it contains " (double quote).. I am not able to create a java object from this json as it is invalidated.
Gson gson = new Gson();
gson.fromJson(json, PasswordResetDetails.java);
Above code snippet is not Working.
If you are doing this for learning / testing purpose all you need to do is escaping the double quote using :
String json="{ "credentials": { "password": "Password\"123", "emailAddress": "skylineadmin#gmail.com" }, "clientTimeMs": 1582006455421, "callerRole": 0 }"
If this is a real scenario then I would like to suggest to change the source in order to make sure it provides valid JSON.
There are countless possibilities to check if your JSON is valid (JSON linting), here's one.
This question already has answers here:
How can I convert JSON to a HashMap using Gson?
(16 answers)
Closed 5 years ago.
I have a problem trying to parse a json file to JSON object
My JSON File is under the form :
{ "objectJSON": [
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
},
{
"attribute":"value",
"attribute":"value",
"attribute":"value",
"attributes" :{
"att1": "val1",
"att2":"val2",
"att3":"val3",
// more atts and vals may figure here !
}
}
]
}
my problem is that the object "attributes" has an unknown number of arguments so I can not create a class for it , I thought about using a
map<String,String> attributes;
but I don't know how to parse it from the file especially that I want to keep my class "ObjectJSON" witch represents the root of my file.
I'm using gson of google
thanks in advance .
Using org.json library:
JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
I have a JSON file which I read from a location and converted it into string as below i.e. A string variable contains exact same JSON as printed below. For some testing purpose I needed it.
So the thing is that I want to update the value of key "MainId". How do I do that?
Here is mine JSON:
{
"Entity": {
"MainId":"XFG",
"AlternateIdentifiers" : [
{
"Type":{
"Abbreviation":"ReferenceNumber"
},
"Value":"abc"
}
]
}
}
Initialize a JSONObject , you have to import the json.org library , put the json in a string and then in the constructor of JSONObject.
So you can navigate the JSON through this new object.
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());
I'm performing a query on my server which returns tree information in JSON format. Below
[{"leaf": 0, "context": {}, "text": "ABC-1-6-1", "expandable": 1, "id": "1.1.2.202.ABC-1-6-1", "allowChildren": 1}]
I was having play round with json-simple library and can parse it ok using JSONParser if can get the above info in String format i.e.
String jsonString = "{\"leaf\": 0, \"context\": {}, \"text\": \"1.1.2.202.ABC-1-6-1\", \"expandable\": 1, \"id\": \"1.1.2.202.ABC-1-6-1\", \"allowChildren\": 1}";
Any help would be great!
If you want to convert JSON to java then use gson. it is very simple.
Person fromJson = new Gson().fromJson(json, Person.class);// this will convert json to java object
fromJson.setAge(15);
fromJson.setName("json");
fromJson.setEmail("email#gmail.com");
fromJson.setMob("4657576876");
json = new Gson().toJson(fromJson);// this will convert java to json string like this {"name":"json","age":15,"email":"email#gmail.com","mob":"4657576876"}
https://sites.google.com/site/gson/gson-user-guide
Refer the following link for json array convertion
serialize and deserialize json array
You have many JSON parsers for Java