简体   繁体   中英

easiest way to access json values in java

Hi I have this commplex JSON. And I only want few values from this JSON. Suppose i want "answers" field values from this json . What can be the easiest method to get these values in java? ARE using jackson or GSON methods the easiest one?

JSON

  [{
            "Id": 1,
            "description": "Contact Information",
            "type": "hidden",
            "subquestions1": [{
                            "Id": "a",
                            "description": "ID",
                            "name": "id",
                            "patternType": "alphanumeric",
                            "required": "false",
                            "answers": "mb999"
            }, {
                            "Id": "b",
                            "description": "Name",
                            "type": "text",
                            "name": "user",
                            "patternType": "text",
                            "required": "false",
                            "length": 100,
                            "answers": "Pope"
            }, {
                            "Id": "c",
                            "description": "Telephone",
                            "type": "text",
                            "length": 10,
                            "name": "telephone",
                            "patternType": "numeric",
                            "required": "false"
            }, {
                            "Id": "d",
                            "description": "Business Unit",
                            "type": "text",
                            "length": 10,
                            "name": "businessUnit",
                            "patternType": "text",
                            "required": "false"
            }, {
                            "Id": "e",
                            "description": "Department",
                            "type": "text",
                            "length": 10,
                            "name": "department",
                            "patternType": "text",
                            "required": "false"
            }, {
                            "Id": "f",
                            "description": "City",
                            "type": "text",
                            "length": 10,
                            "name": "city",
                            "patternType": "text",
                            "required": "false"
            }, {
                            "Id": "g",
                            "description": "State",
                            "type": "text",
                            "length": 10,
                            "name": "state",
                            "patternType": "text",
                            "required": "false"
            }]
},

 {
            "Id": 2,
            "description": "Contact Information",
            "type": "hidden",
            "required": "false",
            "subquestions1": [{
                            "Id": "a",
                            "description": "ID",
                            "type": "text",
                            "name": "id2",
                            "patternType": "alphanumeric",
                            "required": "true",
                            "length": 200,
                            "answers": "mb999"
            }, {
                            "Id": "b",
                            "description": "Name",
                            "type": "text",
                            "name": "user2",
                            "patternType": "text",
                            "required": "false",
                            "length": 100
            }, {
                            "Id": "c",
                            "description": "Telephone",
                            "type": "text",
                            "length": 10,
                            "name": "telephone2",
                            "patternType": "numeric",
                            "required": "false"
            }, {
                            "Id": "d",
                            "description": "Business Unit",
                            "type": "text",
                            "length": 10,
                            "name": "businessUnit2",
                            "required": "false",
                            "patternType": "text"
            }, {
                            "Id": "e",
                            "description": "Department",
                            "type": "text",
                            "length": 10,
                            "name": "department2",
                            "required": "false",
                            "patternType": "text"
            }, {
                            "Id": "f",
                            "description": "City",
                            "type": "text",
                            "length": 10,
                            "name": "city2",
                            "required": "false",
                            "patternType": "text",
                            "answers": "Atlanta"
            }, {
                            "Id": "g",
                            "description": "State",
                            "type": "text",
                            "length": 10,
                            "name": "state2",
                            "required": "false",
                            "patternType": "text"
            }]
},

 {
            "Id": 3,
            "description": "ID information.",
            "type": "hidden",
            "required": "false",
            "subquestions1": [{
                            "Id": "a",
                            "description": "If there is a form",
                            "type": "hidden",
                            "required": "false",
                            "subquestions2": [{
                                            "id": "a.1",
                                            "description": "Work Request",
                                            "type": "text",
                                            "required": "false",
                                            "placeholder": "Please enter"
                            }, {
                                            "id": "a.2",
                                            "description": "Portfolio",
                                            "type": "text",
                                            "required": "false",
                                            "placeholder": "Please enter     Portfolio"
                            }, {
                                            "id": "a.3",
                                            "description": "Primary PID #",
                                            "type": "text",
                                            "required": "false",
                                            "placeholder": "Please enter Primary PID"
                            }, {
                                            "id": "a.4",
                                            "description": "Please list any PID",
                                            "type": "text",
                                            "required": "false",
                                            "placeholder": "Please list any PID"
                            }]
            }]
}, 

{
            "Id": 4,
            "description": "Project Name",
            "name": "projName",
            "type": "textarea",
            "length": 300,
            "required": "true",
            "patternType": "text",
            "placeholder": "Please enter Project Name",
            "answers": "proj"
}, {
            "Id": 5,
            "description": "Doc Nickname",
            "name": "docNickname",
            "type": "textarea",
            "length": 300,
            "required": "false",
            "patternType": "text",
            "placeholder": "Please enter Doc Nickname"
}, {
            "Id": 6,
            "description": "Launch Date",
            "type": "text",
            "required": "true",
            "name": "launchDate",
            "patternType": "Date",
            "placeholder": "Please enter Launch Date",
            "LaunchDate": "2016-01-04T18:30:00.000Z"
 },

{
            "Id": 7,
            "description": "Non-Disclosure Agreement(NDA) Project",
            "type": "radio",
            "required": "true",
            "subquestions1": [{
                            "description": "Yes",
                            "value": "yes",
                            "name": "content",
                            "subquestions2": [{
                                            "Id": "a",
                                            "description": "Does the NDA for this project limit discussion of project details?",
                                            "subquestions3": [{
                                                            "description":   "Yes",
                                                            "value": "yesInner",
                                                            "type": "radio",
                                                            "name": "content1"
                                            }, {
                                                            "description": "No",
                                                            "value": "noInner",
                                                            "type": "radio",
                                                            "name": "content1"
                                            }]
                            }, {
                                            "Id": "b",
                                            "description": "Are Need to Know   Employees required to be covered by an NDA?",
                                            "subquestions3": [{
                                                            "description":  "Yes",
                                                            "value": "yes",
                                                            "type": "radio",
                                                            "name": "content2"
                                            }, {
                                                            "description": "No",
                                                            "value": "no",
                                                            "type": "radio",
                                                            "name": "content2"
                                            }]
                            }, {
                                            "Id": "c",
                                            "description": "If you are currently working with an LC, please select LC name?",
                                            "type": "dropdown",
                                            "repeatSelect": null,
                                            "availableOptions": [{
                                                            "name": "Please Select"
                                            }, {
                                                            "id": 1,
                                                            "name": "Evans,Heidi"
                                            }, {
                                                            "id": 2,
                                                            "name": "Gulledge, Michael"
                                            }, {
                                                            "id": 3,
                                                            "name": "Jones, Nora"
                                            }, {
                                                            "id": 4,
                                                            "name": "Palleras, Maria"
                                            }, {
                                                            "id": 5,
                                                            "name": "Sheehan, Sandra"
                                            }, {
                                                            "id": 6,
                                                            "name": "Thiel, Julie"
                                            }, {
                                                            "id": 7,
                                                            "name": "Younger, Sarah"
                                            }]
                            }]
            },


             {
                            "description": "No",
                            "value": "no",
                            "name": "content"
            }],
            "answers": "no"
}]

I use just org.json

So that whole JSON output is an array of objects because it starts with a [ .

So lets say you store that response in a String, it goes like this.

JSONArray arr = new JSONArray(response); //response is the String

Now you have a JSON array. To get each object, you would loop it.

for (int i = 0; i < arr.length(); i++) {
     JSONObject obj = arr.getJSONObject(i);
}

There's your object.

If you want to get the value of "Id", you would do this

for (int i = 0; i < arr.length(); i++) {
     JSONObject obj = arr.getJSONObject(i);
     obj.getInt("Id");
}

and you get the values 1,2,3,4,5,6,7

Hope this helps.

EDIT: I edited the getInt(). Read the json wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM