2013-03-21 80 views
0

我想創建一個json一個jsonArray,但我迷失在做它。Initialse a jsonArray java

我在哪裏錯了,我們如何initiale JSON數組

JSONArray template = 
      { 
        "header": "Colors", 
        "items": [ 
         {"name": "red", "first": true, "url": "#Red"}, 
         {"name": "green", "link": true, "url": "#Green"}, 
         {"name": "blue", "link": true, "url": "#Blue"} 
        ], 
        "empty": false 
       }; 
+0

上面是一個jsonobject有一個JSONArray「items」。 – 2013-03-21 06:37:06

+1

json數組以[和結束]開頭 – 2013-03-21 06:37:11

+0

首先,這是一個JSONObject,正如評論已經說過的那樣。其次,你需要新的對象 - 你不能像這樣創建一個Java對象。 – 2013-03-21 06:43:51

回答

0

您需要創建模板的JSONObject提到的,它會自動初始化items

try{ 
    String template = "{\"header\": \"Colors\", " + 
      "\"items\": [ " + 
      "{\"name\": \"red\", \"first\": true, \"url\": \"#Red\"}, " + 
      "{\"name\": \"green\", \"link\": true, \"url\": \"#Green\"}, " + 
      "{\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"}" + 
      " ], \"empty\": false }"; 

    JSONObject jsonWithArrayInIt = new JSONObject(template); //JSONObject created for the template. 
    JSONArray items = jsonWithArrayInIt.getJSONArray("items"); //JSONArray of Items got from the JSONObject. 

    System.out.println(items.toString()); 

    }catch(JSONException je){ 
     //Error while creating JSON. 
} 
0

以下是正確的方法來初始化一個JSONArray

public class TestJSON { 

public static void main(String[] args) { 
    JSONArray template = new JSONArray("[ {\"name\": \"red\", \"first\": true, \"url\": \"#Red\"}," + 
      " {\"name\": \"green\", \"link\": true, \"url\": \"#Green\"}," + 
      "{\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"} ]"); 

    System.out.println(template.toString()); 

    } 
} 

以下是輸出:

[ 
    { 
     "name": "red", 
     "first": true, 
     "url": "#Red" 
    }, 
    { 
     "link": true, 
     "name": "green", 
     "url": "#Green" 
    }, 
    { 
     "link": true, 
     "name": "blue", 
     "url": "#Blue" 
    } 
] 

EDIT1:

您可以使用下面的代碼來創建完整的JSON對象..

public static void main(String[] args) { 
     JSONArray template = new JSONArray(
      "[ {\"name\": \"red\", \"first\": true, \"url\": \"#Red\"}," 
       + " {\"name\": \"green\", \"link\": true, \"url\": \"#Green\"}," 
       + "{\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"} ]"); 

     JSONObject object = new JSONObject(); 
     object.put("header", "Colors"); 
     object.put("empty", false); 
     object.put("items", template); 

     System.out.println(object.toString()); 
     } 

以下是輸出:

{ 
    "items": [ 
     { 
      "name": "red", 
      "first": true, 
      "url": "#Red" 
     }, 
     { 
      "link": true, 
      "name": "green", 
      "url": "#Green" 
     }, 
     { 
      "link": true, 
      "name": "blue", 
      "url": "#Blue" 
     } 
    ], 
    "empty": false, 
    "header": "Colors" 
} 

EDIT2:

下面的代碼可用於生成完整不使用包含JSON數據的字符串的JSON對象:

public static void main(String[] args) { 
    JSONArray template = new JSONArray(); 

    JSONObject obj = new JSONObject(); 
    obj.put("name", "red"); 
    obj.put("first", true); 
    obj.put("url", "#Red"); 
    template.put(obj); 

    JSONObject obj1 = new JSONObject(); 
    obj1.put("name", "green"); 
    obj1.put("link", true); 
    obj1.put("url", "#Green"); 
    template.put(obj1); 

    JSONObject obj2 = new JSONObject(); 
    obj2.put("name", "blue"); 
    obj2.put("link", true); 
    obj2.put("url", "#Blue"); 
    template.put(obj2); 

    JSONObject object = new JSONObject(); 
    object.put("header", "Colors"); 
    object.put("empty", false); 
    object.put("items", template); 

    System.out.println(object.toString()); 
    } 

以下是該程序的輸出:

{ 
    "items": [ 
     { 
      "name": "red", 
      "first": true, 
      "url": "#Red" 
     }, 
     { 
      "link": true, 
      "name": "green", 
      "url": "#Green" 
     }, 
     { 
      "link": true, 
      "name": "blue", 
      "url": "#Blue" 
     } 
    ], 
    "empty": false, 
    "header": "Colors" 
} 
0

,而不是自我的格式,我已經做了以下面的方式,它爲我工作。

function fillPolicydetails() 
    { 
    var clsPolicyDetails={} 
     clsPolicyDetails.name="Sangeeta" 
     clsPolicyDetails.technology=".net" 
    return clsPolicyDetails; 
    } 




    $.ajax({ 
       type: "POST", 
       url: url, 
       data: "{'policydetail':" + JSON.stringify(fillPolicydetails())+"}", //finally here's the magic: 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       async: false, 
       success: function (Result) { 
        successFunction(Result); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) { 
        failureFunction(jqXHR, textStatus, errorThrown); 
       } 
      }); 

上面的代碼會爲你生成JSON格式,並將其發送作爲對象,而不是原始字符串

,如果任何問題,請讓我知道。

0

如果模板將是JSON數組是指持有超過一個值,則初始化是這樣的:

JSONArray template = new JSONArray(" 
     [{ 
       "header": "Colors", 
       "items": [ 
        {"name": "red", "first": true, "url": "#Red"}, 
        {"name": "green", "link": true, "url": "#Green"}, 
        {"name": "blue", "link": true, "url": "#Blue"} 
       ], 
       "empty": false 
      }]"); 

如果項目是JSON數組和模板是一個JSON對象持有的項目然後做這樣的

JSONArray items=new JSONArray ("[ 
         {"name": "red", "first": true, "url": "#Red"}, 
         {"name": "green", "link": true, "url": "#Green"}, 
         {"name": "blue", "link": true, "url": "#Blue"} 
        ]"); 


JSONObject template = new JSONObject(); 
template .put("sessionId", 
        new JSONString("Colors")); 
template .put("items", 
        items); 
template .put("empty", 
        new JSONBoolean(false)); 

有時邏輯正確,其功能名稱或類名會根據不同的包改變。我正在使用com.google.gwt.json.client