Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android应用中的JSON解析_Android_Json - Fatal编程技术网

android应用中的JSON解析

android应用中的JSON解析,android,json,Android,Json,我是android新手,我有JSON对象如何解析我的JSONarray [ { "Men":, "shirts": [ { "name":"ABC", "image":"http://domain.com/image.jpg" }, { "name":"USA",

我是android新手,我有
JSON
对象如何解析我的
JSONarray

[
    {
        "Men":,
        "shirts": [
            {
                "name":"ABC",
                "image":"http://domain.com/image.jpg"
            },
            {
                "name":"USA",
                "image":"imageURLg"
            }
        ],
        "Pants": [
            {
                "name":"sample",
                "image":"imageurl"
            },
            {
                "name":"shoper",
                "image":"imageurl"
            }
        ]
    }
]

请告诉我如何将数组拆分为多个类别

json解析的简单技巧:

找到这个大括号“{”,然后得到JsonObject


在这里找到这个大括号“[”,然后获取JsonArray。

要解析JSON,请使用JsonArray类以“[”开头

JSONArray jsonArray = new JSONArray (response)
在数组对象中迭代以逐项获取

for (int i = 0; i < array.length(); i++) {
    JSONObject row = jsonArray.getJSONObject(i);
    value = row.getString(TAG);
}
for(int i=0;i
这是一个关于JSON解析的非常完整的教程: 试试这个

JSONObject jsonObject = new JSONObject(here give the name of your json string);
JSONArray jsonArray = new JSONArray(jsonObject.getJSONArray("shirts"));

然后循环通过jsonArray

检查JSON是否无效。请确保始终签入JSON。这是使用修改后的JSON将其拆分为类别的方法:

String jsonString = "[\n" +
                "    {\n" +
                "        \"Men\": \"\",\n" +
                "        \"shirts\": [\n" +
                "            {\n" +
                "                \"name\": \"ABC\",\n" +
                "                \"image\": \"http://domain.com/image.jpg\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\": \"USA\",\n" +
                "                \"image\": \"imageURLg\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"Pants\": [\n" +
                "            {\n" +
                "                \"name\": \"sample\",\n" +
                "                \"image\": \"imageurl\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\": \"shoper\",\n" +
                "                \"image\": \"imageurl\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "]";
        try{
            JSONArray array = new JSONArray(jsonString);
            //Get the shirts
            JSONArray shirtsArray = array.getJSONObject(0).getJSONArray("shirts");
            Log.d(LOG_TAG,"shirtsArray = " + shirtsArray.toString());
            for (int i = 0; i < shirtsArray.length(); i++){
                Log.d(LOG_TAG, "name = " + shirtsArray.getJSONObject(i).getString("name")+" image: "+shirtsArray.getJSONObject(i).getString("image"));
            }
            //Get the pants
            JSONArray pantsArray = array.getJSONObject(0).getJSONArray("Pants");
            Log.d(LOG_TAG,"shirtsArray = " + shirtsArray.toString());
            for (int i = 0; i < pantsArray.length(); i++){
                Log.d(LOG_TAG, "name = " + pantsArray.getJSONObject(i).getString("name")+" image: "+pantsArray.getJSONObject(i).getString("image"));
            }
        }catch (JSONException ex){
            Log.e(LOG_TAG, ex.toString());
        }
String jsonString=“[\n”+
“{\n”+
“\”男人“:\“\”,\n”+
“\'shirts\”:[\n”+
“{\n”+
“名称”:“ABC\,\n”+
“\”图像\“:\”http://domain.com/image.jpg\“\n”+
},\n+
“{\n”+
“\”名称\“:\”美国\“,\n”+
“\”图像\“:\”图像URLG\“\n”+
“}\n”+
“],\n”+
“\“裤子\”:[\n”+
“{\n”+
“\”名称“:\”样本“,\n”+
“\”图像\“:\”图像URL\“\n”+
},\n+
“{\n”+
“\”姓名\“:\”购物者\“,\n”+
“\”图像\“:\”图像URL\“\n”+
“}\n”+
“]\n”+
“}\n”+
"]";
试一试{
JSONArray数组=新的JSONArray(jsonString);
//去拿衬衫
JSONArray shirtsArray=array.getJSONObject(0.getJSONArray(“衬衫”);
Log.d(Log_标签,“shirtsArray=“+shirtsArray.toString());
对于(int i=0;i
首先,发布一个有效格式的Json。就像@Rami said一样,发布格式化的Json。使用JsonArray类解析。JsonArray数组=新的JsonArray(jsonObjectName);您的JSON完全无效,但如果您正确格式化,您会注意到。元素之间缺少冒号,也缺少一些括号。发现此答案有帮助??请提及。如果没有,请发表评论。因此,我们可以为您提供进一步的帮助。帮助我如何将JSON数组插入categories@israr ahmad