2017-05-18 69 views
0

有人可以提供我的Java代碼來創建一個JSON對象如下圖所示在Java中創建一個JSON數據對象

{"main":[ 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"] 
]} 

我已經試過類似

 Gson gson = new Gson(); 
     JsonArray array = new JsonArray(); 

     array.add(new JsonPrimitive("One")); 
     array.add(new JsonPrimitive("two")); 
     array.add(new JsonPrimitive("three")); 
     array.add(new JsonPrimitive("four")); 
     array.add(new JsonPrimitive("five")); 

     JsonObject jsonObject = new JsonObject(); 
     jsonObject.add("main", array); 

我越來越像下面的結果甚至當我在循環

{"main":["one","two","three","four","five"]} 

像一個單一的對象。但我期待的結果像

{"main":[ 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"], 
     ["one","two","three","four","five"] 
]} 

非常感謝提前。

回答

0

看來"main"包含數組的數組,所以你必須做的就是添加你array五次到另一個新的陣列(例如,稱之爲mainArray),然後添加mainArrayjsonObject

  • 創建一個新的空數組:mainArray
  • 添加array五次mainArray
  • 添加mainArrayjsonObject
0

你可以嘗試將字符串轉換代表你的JSON數據的JSONObject:

import com.google.gson.JsonObject; 
import com.google.gson.JsonParser; 


public class JsonQuestion { 

    public static void main(String[] args) { 

     String myJSON = "{\"main\":[\n" 
       + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n" 
       + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n" 
       + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n" 
       + " [\"one\",\"two\",\"three\",\"four\",\"five\"],\n" 
       + " [\"one\",\"two\",\"three\",\"four\",\"five\"]\n" 
       + "]}"; 

     JsonParser jsonParser = new JsonParser(); 
     JsonObject jsonObject = (JsonObject) jsonParser.parse(myJSON); 
     System.out.println("jsonObject: " + jsonObject.toString()); 

    } 
} 
1

試試這個代碼來創建JSON

Gson gson = new Gson(); 
JsonArray array = new JsonArray(); 
JsonArray child = new JsonArray(); 

child.add(new JsonPrimitive("One")); 
child.add(new JsonPrimitive("two")); 
child.add(new JsonPrimitive("three")); 
child.add(new JsonPrimitive("four")); 
child.add(new JsonPrimitive("five")); 

for(int i=0;i<5;i++) 
array.add(child); 


JsonObject jsonObject = new JsonObject(); 
jsonObject.add("main", array); 

System.out.println(jsonObject); 
0
Gson gson = new Gson(); 
    JsonArray array = new JsonArray(); 

    array.add(new JsonPrimitive("One")); 
    array.add(new JsonPrimitive("two")); 
    array.add(new JsonPrimitive("three")); 
    array.add(new JsonPrimitive("four")); 
    array.add(new JsonPrimitive("five")); 

    JsonObject jsonObject = new JsonObject(); 

    JsonArray marray = new JsonArray(); 
    marray.add(array); 
    marray.add(array); 
    marray.add(array); 
    marray.add(array); 
    marray.add(array); 
    jsonObject.add("main", marray); 
0

您可以使用下面的方法來使JSON:

private void createJsonData(){ 
     final String[] units = {"One","Two","Three","Four", 
       "Five"}; 
     try { 
     JSONObject jsonObject = new JSONObject(); 
     JSONArray jsonArray = new JSONArray(); 

      JSONArray jsonArray1 = new JSONArray(); 
      for (int j = 0; j < 5 ; j++) { 
       jsonArray1.put(units[j]) 
      } 
      jsonArray.put(jsonArray); 

      jsonObject.put("main",jsonArray); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
1

假設你在使用Gson, s不是它被設計使用的方式。儘管支持這種方式,但並不建議這樣做,因爲您可以使用任何json庫來執行此操作(SimpleJson)。但是,Gson能夠直接序列化我們熟悉的java對象,因此您應該將您的json對象表示爲java對象。 JsonObject映射到一個Map。 JsonArray映射到List或一個數組。 JsonPrimitives映射到它們各自的Java基本類型(布爾,雙精度,字符串,空)

// generate the object 
Map<List<List<String>>> object = new HashMap<>(); 
List<List<String>> main = new ArrayList<>(); 
List<String> counts = Arrays.asList("one", "two", "three", "four", "five"); 
for (int i = 0; i < 5; i++) { 
    main.add(counts); 
} 
object.put("main", main); 

// serialize it 
String json = new Gson().toJson(object); 

// deserializing it requires a typetoken or separate class representing the map object. 
Map<List<List<String>>> desObj = new Gson().fromJson(json, new TypeToken<Map<List<List<String>>>>(){}.getType());