Android 初始化jsonArray java

Android 初始化jsonArray java,android,json,Android,Json,我试图在json和jsonArray之间初始化一个json,但我在做这件事时迷失了方向 我错在哪里?如何初始化json数组 JSONArray template = { "header": "Colors", "items": [ {"name": "red", "first": true, "url": "#Red"},

我试图在json和jsonArray之间初始化一个json,但我在做这件事时迷失了方向

我错在哪里?如何初始化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
                };

以下是初始化JSONArray的正确方法

输出结果如下:

[
    {
        "name": "red",
        "first": true,
        "url": "#Red"
    },
    {
        "link": true,
        "name": "green",
        "url": "#Green"
    },
    {
        "link": true,
        "name": "blue",
        "url": "#Blue"
    }
]
{
    "items": [
        {
            "name": "red",
            "first": true,
            "url": "#Red"
        },
        {
            "link": true,
            "name": "green",
            "url": "#Green"
        },
        {
            "link": true,
            "name": "blue",
            "url": "#Blue"
        }
    ],
    "empty": false,
    "header": "Colors"
}
编辑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());
        }
输出结果如下:

[
    {
        "name": "red",
        "first": true,
        "url": "#Red"
    },
    {
        "link": true,
        "name": "green",
        "url": "#Green"
    },
    {
        "link": true,
        "name": "blue",
        "url": "#Blue"
    }
]
{
    "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"
}
您需要创建上述模板的JSONObject,它将自动初始化这些项


我用下面的方法来做,而不是自我格式化,它对我很有效

  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格式,并将其作为对象而不是原始字符串发送


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

如果模板是您的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是一个包含items的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

上面是一个jsonobject,它有一个JSONArray项。json数组首先以[和]结尾,正如前面的评论所说,这是一个jsonobject。第二,你需要创建一个新的对象——你不能像那样创建一个java对象point@yakusha这是jsonobject,它有一个json数组,您希望这样,然后将其初始化为jsonobject