Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
Java中解析JSON的问题_Java_Json - Fatal编程技术网

Java中解析JSON的问题

Java中解析JSON的问题,java,json,Java,Json,我有一个json: [{ "name": "prog 1", "show": [{ "name": "n1", "time": "01.10 " }, { "name": "n2", "time": "01.35 " }] }, { "name": "prog 2", "show": [{ "name": "n1", "time": "01.10 "

我有一个json:

[{
    "name": "prog 1",
    "show": [{
        "name": "n1",
        "time": "01.10 "
    }, {
        "name": "n2",
        "time": "01.35 "
    }]
}, {
    "name": "prog 2",
    "show": [{
        "name": "n1",
        "time": "01.10 "
    }, {
        "name": "n2",
        "time": "01.35 "
    }]
}]
现在尝试用Java解析它,如:

JSONObject json=new JSONObject(json_str);

抛出一个异常,因为它不是以{开头,而是[因为它是一个数组。我可以在js中毫无问题地解析它,但是我不能用这个字符串加载JSONArray…

使用:
JSONArray objArray=newJSONArray(json_str);

//要访问数组中的单个对象:

对于(int i=0;i您是否尝试过:

    JSONArray arr = new JSONArray(stringWithContent);
然后像这样访问它:

    for(int i = 0; i<arr.length();i++){
        System.out.println(arr.get(i));
    }

for(inti=0;i您可以尝试以下代码

JSONObject jObject  = new JSONObject(json_str);
JSONArray array = jObject.getJSONArray("show");    
for(int i = 0 ; i < array.length() ; i++)
{
    System.out.println(array.getJSONObject(i).getString("name"));
    System.out.println(array.getJSONObject(i).getString("time"));
}
JSONObject jObject=新的JSONObject(json_str);
JSONArray数组=jObject.getJSONArray(“show”);
对于(int i=0;i

这将很有帮助…

检查此->如果它能提供帮助。有时,如果您不仅描述您遇到的异常,而且在问题中包含stacktrace,我们会更容易为您提供帮助。输入包含一个数组,而不是一个对象。请访问json.org并研究语法。您有一个对象数组(对象包含数组).您需要使用JSONArray。
JSONObject jObject  = new JSONObject(json_str);
JSONArray array = jObject.getJSONArray("show");    
for(int i = 0 ; i < array.length() ; i++)
{
    System.out.println(array.getJSONObject(i).getString("name"));
    System.out.println(array.getJSONObject(i).getString("time"));
}