2016-01-25 52 views
0

嗨,我有這個commplex JSON。我只想從這個JSON中得到一些值。假設我想從這個json得到「答案」字段值。在java中獲取這些值最簡單的方法是什麼?使用傑克遜或GSON方法是最簡單的方法嗎?在java中訪問json值的最簡單方法

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" 
}] 
+1

是的,我可能會使用JSON API將JSON反序列化爲一種DOM表示,然後導航。你嘗試過嗎?目前不清楚你在問什麼。 –

+0

我只是問最簡單的方法從複雜的json中獲取值。 – user3449214

+0

添加@JonSkeet的評論,你可以使用http://www.json.org/javadoc/org/json/JSONObject.html創建一個JSON對象,然後調用object.get(key)來獲得值 – Limit

回答

0

我只使用org.json

讓整機JSON輸出對象的數組,因爲它有一個[開始。

因此,讓我們說你將該響應存儲在一個字符串中,它是這樣的。

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錯誤

相關問題