Reputation: 87
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!
Upvotes: 1
Views: 787
Reputation: 2073
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.
Upvotes: 0
Reputation: 806
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"));
}
Upvotes: 2
Reputation: 922
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/
Upvotes: 6