1

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!

asked Sep 25, 2015 at 9:34
2
  • Read about jackson library Commented Sep 25, 2015 at 9:36
  • mark my answer as answered, so if someone will check out this question, will find answer immediatly Commented Sep 25, 2015 at 9:58

3 Answers 3

6
answered Sep 25, 2015 at 9:36
Sign up to request clarification or add additional context in comments.

Comments

2

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")); 
 } 
answered Sep 25, 2015 at 9:45

2 Comments

I think GSON might be simplier for newbie
Thank you very much! @vikas balyan
0

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.

answered Sep 25, 2015 at 9:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.