嗨,我有这个复杂的JSON。我只想从这个JSON中获得很少的值。假设我想要"答案"来自这个json的字段值。在java中获取这些值的最简单方法是什么?使用jackson或GSON方法最简单吗?
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"
}]
答案 0 :(得分:0)
我只使用org.json
因此整个JSON输出是一个对象数组,因为它以[
开头。
因此,假设您将该响应存储在String中,就像这样。
JSONArray arr = new JSONArray(response); //response is the String
现在你有一个JSON数组。要获取每个对象,您可以循环它。
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
}
有你的对象。
如果您想获得“Id”的值,您可以这样做
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
obj.getInt("Id");
}
您将获得值1,2,3,4,5,6,7
希望这有帮助。
编辑:我编辑了getInt()。读json错误