1

i have trouble in parsing json

I have a nested json string, i need to convert to java object , the string look like this, i want to ask how to handle this nested dynamic json using jackson, how to decode dynamic nested json string

{

"resultCode": "0",
"dataObject": [
    {
        "lastSyncDate": "20140101000000",
        "count": 2,
        "refType": "ADO",
        "update": [
            {
                "artist": "C",
                "albumTitle": "道",
                "productTitle": "道",
                "thumbnail": "http://w.com/mposter/album/m/VACL00020880A_m.jpg",
                "lastSyncDate": "20140425120159",
                "refId": "VACL00214522"
            },
            {
                "artist": "楊",
                "albumTitle": "學",
                "productTitle": "美",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20140324161831",
                "refId": "VACP00168673"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 8,
        "refType": "PAT",
        "update": [
            {
                "artist": "方",
                "thumbnail": "http://t.com/moov/images/profile/PAT/8/70/00021870_tn_1_s.jpg",
                "lastSyncDate": "20140201010203",
                "refId": "00021870"
            },
            {
                "artist": "楊",
                "lastSyncDate": "20140328120831",
                "refId": "00000125"
            },
            {
                "artist": "陳",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140328185030",
                "refId": "00017704"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 4,
        "refType": "PAB",
        "update": [
            {
                "artist": "陳",
                "albumTitle": "The Key",
                "thumbnail": "http:/m.jpg",
                "lastSyncDate": "20140603143528",
                "refId": "VAUN00031629A"
            },
            {
                "artist": "何",
                "albumTitle": "梁",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20140603143528",
                "refId": "VAEA00003170A"
            },
            {
                "artist": "何",
                "albumTitle": "艷",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20110603151452",
                "refId": "VAEA00003179A"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 4,
        "refType": "PP",
        "update": [
            {
                "chiName": "其",
                "engName": "Other",
                "lastSyncDate": "20140130010203",
                "chiAuthor": "",
                "engAuthor": "",
                "refId": "PP1000000003"
            },
            {
                "chiName": "演",
                "engName": "E演",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140126010758",
                "chiAuthor": "專",
                "engAuthor": "Recommended",
                "refId": "PP1000000040"
            },
            {
                "chiName": "日本派台歌",
                "engName": "Japan New Releases",
                "lastSyncDate": "20140126010758",
                "chiAuthor": "",
                "engAuthor": "",
                "refId": "PP1000000057"
            },
            {
                "chiName": "9",
                "engName": "9",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140126010203",
                "chiAuthor": "專",
                "engAuthor": "Recommended",
                "refId": "PP1000000048"
            }
        ],
        "delete": [ ]
    }
]

}
1
  • please help me , the hard part is the field of Update is not fixed Commented Aug 5, 2014 at 3:29

2 Answers 2

3

You only need to reproduce the structure of the json using java class structure. For instance, for your case:

    public class Result {
        String resultCode;
        List<DataObject> dataObjects;
        <GETTERS & SETTERS>
    }

    public class DataObject {
        String lastSyncDate;
        int count;
        String refType;
        List<Update> updates;
        List<Delete> deletes;
        <GETTERS & SETTERS>
    }

    public class Update {
        String artist;
        String albumTitle;
        String productTitle;
        String thumbnail;
        String lastSyncDate;
        String refId;
        <GETTERS & SETTERS>
    }

    public class Delete {
        String refId;
        <GETTERS & SETTERS>
    }

I assumed that Delete class only contains the refId. With this structure, you'll be able to map the json to an object of the class Result without any problems doing:

    byte[] jsonData = <YOUR JSON>
    ObjectMapper objectMapper = new ObjectMapper();
    Result result = objectMapper.readValue(jsonData, Result.class);
Sign up to request clarification or add additional context in comments.

1 Comment

ok, but the number of field in "Update" is different in each json object, how to hande it??
2

Since the field of Update is not fixed, use map instead of the Update class.

    public class Result {
        String resultCode;
        List<DataObject> dataObject;
        <GETTERS & SETTERS>
    }

    public class DataObject {
        String lastSyncDate;
        int count;
        String refType;
        List<Map<String, String>> update;
        List<String> delete;
        <GETTERS & SETTERS>
    }

Assume that update only contains String values.

    String jsonData = "YOUR JSON";
    ObjectMapper objectMapper = new ObjectMapper();
    Result result = objectMapper.readValue(jsonData, Result.class);

3 Comments

Thx, more details on Map class ok??? any getting or setting function need to code in the map class???
and How to code getting and setting in the superclass in order to get value from sub class
for update: private List<Map<String, String>> getUpdate() {return update;} private void setUpdate(List<Map<String, String>> update) {this.update = update;}. Just like other fields.

Your Answer

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.