2

我正在尝试将 json 初始化为 jsonArray,但我迷失了。

我在哪里错了,我们如何初始化一个 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
                };
4

4 回答 4

1

您需要创建一个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 present inside the JSONObject.
    System.out.println(items.toString());
} catch (JSONException je) {
    //Error while creating JSON.
}
于 2013-03-21T06:50:51.687 回答
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"
    }
]

编辑1:

您可以使用以下代码创建完整的 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"
}

编辑2:

以下代码可用于生成完整的 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"
}
于 2013-03-21T06:48:32.340 回答
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 格式并将其作为对象而不是原始字符串发送

如果有任何问题,请告诉我。

于 2013-03-21T06:52:35.103 回答
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
            }]");

如果 items 是 json 数组并且 template 是一个 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

于 2013-03-21T06:58:04.687 回答