2016-05-06 12 views
-2

Von dem, was ich gelesen habe das folgende Beispiel ist ein JSONArray.Programmatisch ein komplexes JSONArray erstellen?

Ich bin sehr verwirrt über das Hinzufügen der oberen "Header" Informationen und der verschachtelten "Position" JSONObject als letzte Eigenschaft. Ich weiß, wie ein grundlegendes JSONObject erstellt wird, sowie ein grundlegendes JSONArray, aber dieses kombinierte wirft mich wirklich ab.

{ 
    "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

Was ist Ihre Frage, die Sie brauchen zu beantworten? – ManoDestra

+0

Der obige Beispielcode muss ich erstellen. Ich weiß nicht, wie ich es mit Java-Code implementieren soll. – SirFerret

+0

Möchten Sie ein JSON-Objekt mit einer bekannten oder einer eigenen Implementierung erstellen? – MaxG

Antwort

2

Versuchen Sie folgendes:

 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

Vielen Dank dafür. Es funktioniert großartig und die Struktur ist sehr klar und leicht zu verstehen. – SirFerret

0

Sie können auch wie folgt tun ..

 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

die große oracle documentation Versuchen. Sie haben ein Beispiel, das zeigt, wie man auch Arrays erstellt. Hier ist die Vorlage sie erstellen möchten:

{ 
    "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" } 
    ] 
} 

Und das ist, wie sie es tun:

\\ 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

Danke, dass du mich das wissen lässt. Ich werde mich besser bemühen. – MaxG