2016-05-06 64 views
-2

從我讀過的下面的例子是JSONArray以編程方式創建複雜的JSONArray?

我最困惑的是添加頂部「標題」信息和嵌套的「位置」JSONObject作爲最後一個屬性。我知道如何創建一個基本的JSONObject,以及一個基本的JSONArray,但是這個結合起來真的讓我失望。

{ 
    "source": "REMOTE", 
    "msgType": "event", 
    "properties": [ 
     { 
      "IMEI": { 
       "string": "1234567890" 
      } 
     }, 
     { 
      "My Time": { 
       "string": "5/4/2016 12:00:00" 
      } 
     }, 
     { 
      "Position": { 
       "geographicPosition": { 
        "latitude": 34.89767579999999, 
        "longitude": -72.03648269999997 
       } 
      } 
     } 
    ] 
} 
+0

什麼是你的問題,你需要回答? – ManoDestra

+0

上面的示例代碼是我需要創建的。我不知道如何用Java代碼實現它。 – SirFerret

+0

是否要使用已知實現或您自己的方法創建JSON對象? – MaxG

回答

2

試試這個:

 try { 
      JSONObject mainObject = new JSONObject(); 
      mainObject.put("source", "REMOTE"); 
      mainObject.put("msgType", "event"); 
      JSONArray mainArray = new JSONArray(); 

      JSONObject arrayObj = new JSONObject(); 
      JSONObject temp = new JSONObject(); 
      temp.put("string", "1234567890"); 
      arrayObj.put("IMEI", temp); 
      mainArray.put(arrayObj); 

      arrayObj = new JSONObject(); 
      temp = new JSONObject(); 
      temp.put("string", "5/4/2016 12:00:00"); 
      arrayObj.put("My Time", temp); 
      mainArray.put(arrayObj); 

      arrayObj = new JSONObject(); 
      temp = new JSONObject(); 
      JSONObject temp1 = new JSONObject(); 
      temp1.put("latitude",34.89767579999999); 
      temp1.put("longitude",-72.03648269999997); 
      temp.put("geographicPosition", temp1); 
      arrayObj.put("Position", temp); 
      mainArray.put(arrayObj); 

      mainObject.put("properties", mainArray); 
      // mainOject is your required Json 
      System.out.println(mainObject); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
+0

非常感謝你。它效果很好,結構非常清晰,易於理解。 – SirFerret

0

你也可以做這樣的..

 JSONObject jsonObj = new JSONObject(json_response); 
     String source = jsonObj.getString("source"); 
     String msgType = jsonObj.getString("msgType"); 
     JSONArray jsonArray = jsonObj.getJSONArray("properties");    
     JSONObject IMEIObject = jsonArray.getJSONObject("IMEI"); 
     String IMEI = IMEIObject.getJSONString("string"); 
     JSONObject myTimeObject = jsonArray.getJSONObject("My Time"); 
     String myTime = myTimeObject.getJSONString("string"); 
     JSONObject posObject = jsonArray.getJSONObject("Position"); 
     JSONObject lljsonObject = posObject.getJSONObject("geographicPosition"); 
     String latitude = lljsonObject.getJSONString(String.valueOf("latitude")); 
     String longitude = lljsonObject.getJSONString(String.valueOf("longitude")); 
0

嘗試偉大oracle documentation。他們有一個例子展示瞭如何創建數組。下面是他們想要創建的模板:

{ 
    "firstName": "John", "lastName": "Smith", "age": 25, 
    "address" : { 
     "streetAddress": "21 2nd Street", 
     "city": "New York", 
     "state": "NY", 
     "postalCode": "10021" 
    }, 
    "phoneNumber": [ 
     { "type": "home", "number": "212 555-1234" }, 
     { "type": "fax", "number": "646 555-4567" } 
    ] 
} 

這就是他們如何做到這一點:

\\ You can pass `null` to the `config` 
JsonBuilderFactory factory = Json.createBuilderFactory(config); 
JsonObject value = factory.createObjectBuilder() 
    .add("firstName", "John") 
    .add("lastName", "Smith") 
    .add("age", 25) 
    .add("address", factory.createObjectBuilder() 
     .add("streetAddress", "21 2nd Street") 
     .add("city", "New York") 
     .add("state", "NY") 
     .add("postalCode", "10021")) 
    .add("phoneNumber", factory.createArrayBuilder() 
     .add(factory.createObjectBuilder() 
      .add("type", "home") 
      .add("number", "212 555-1234")) 
     .add(factory.createObjectBuilder() 
      .add("type", "fax") 
      .add("number", "646 555-4567"))) 
    .build(); 
+0

謝謝你讓我知道。我會做更好的努力。 – MaxG