2017-10-29 182 views
-1

我得到一个InputStream并将其转化为String,然后JSONObject无法字符串转换为JSONObject的

下面是摘录转换的InputStream String,然后JSONObject的。

但转换成JSON对象(6号线),我只得到了第一object,而不是所有的objects

可否请您让我知道后,为什么我只得到一个对象,而不是所有的n 对象

InputStream in = new BufferedInputStream(conn.getInputStream()); 
String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); 
int i =result.indexOf("{"); 
String forResult=result.substring(i); 
System.out.println(forResult); // Result 1 
JSONObject jsonObject = new JSONObject(forResult); // Line 6 
System.out.println(jsonObject); // Result 2 

将其转换为字符串后,它看起来像这样

结果-1

{ 
    "test_expr":"", 
    "val_expr":"someVale", 
    "val_advanced":true, 
    "machine_val":null 
}, {...// n times} 

结果 - 2 - 仅第一个对象

{ 
    "test_expr":"", 
     "val_expr":"someVale", 
     "val_advanced":true, 
     "machine_val":null 
    } 

谢谢,请大家多多包涵我的无知,因为我在Java

+1

'JSONObject'不会进一步比封闭'}'解析。流中的内容是否是JSON数组?你为什么子串? –

+0

@SotiriosDelimanolis在使用'org.apache.commons.io.IOUtils.toString(in,“UTF-8”)转换它后,它看起来像这个[{...},{...} ...]; ' – brk

+0

好吧,它是一个JSON数组。使用'JSONArray'解析它。 –

回答

0

完全地新的因为你JSON是不有效。在JSONObject之间有一个逗号。

更改为此。

{ 
    { 
    "test_expr":"", 
    "val_expr":"someVale", 
    "val_advanced":true, 
    "machine_val":null 
    }, 
    { 
    "test_expr":"", 
    "val_expr":"someVale", 
    "val_advanced":true, 
    "machine_val":null 
    } 
    ... 
} 

[ 
    { 
    "test_expr":"", 
    "val_expr":"someVale", 
    "val_advanced":true, 
    "machine_val":null 
    }, 
    { 
    "test_expr":"", 
    "val_expr":"someVale", 
    "val_advanced":true, 
    "machine_val":null 
    } 
    ... 
] 

JSONObject

/** 
* Creates a new {@code JSONObject} with name/value mappings from the JSON 
* string. 
* 
* @param json a JSON-encoded string containing an object. 
* @throws JSONException if the parse fails or doesn't yield a {@code 
*  JSONObject}. 
*/ 
public JSONObject(String json) throws JSONException { 
    this(new JSONTokener(json)); 
} 

源SO含有的对象的JSON编码的字符串(如{})。

// make sure that you result contain {} 
result = "{your data here}" 
JSONObject json_data = new JSONObject(result); 

如果您使用JSONArray,你应该在你JSON

result = "[json data]"; 
JSONArray jsonArray = new JSONArray(result); 
+0

为什么要投票? – KeLiuyue

+0

如果他们的JSON无效,那么'JSONObject'将无法解析它。你的第一个片段实际上是无效的。请在发布之前验证您的答案。 –

+0

我还注意到你的答案的最后一部分是你的另一个最近的答案的副本。如果两个问题具有相同的答案,则应将其视为重复项,并且应该投票决定如何关闭它们。您有权访问该特权并使用它。 –

0

嘛包含[],多jsons的拼接不是有效的JSON。你的解析库应该已经拒绝了这样的输入,但它似乎只是停在有效对象的末尾。

你可以将它列表包装成一个数组:[{...},{...},{...}]。然后解析器将能够正确地将其解释为一个数组。

0

我想你会得到JSONArray对象而不是JSONObject。为什么你需要得到结果的子字符串? Json数组可以以[开头。查看JSONObject和JSONArray之间的区别。 Difference between JSONObject and JSONArray

0

感谢Sotirios Delimanolis。

我能解决这个问题,通过使用JSONParser

InputStream in = new BufferedInputStream(conn.getInputStream()); 
     String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8"); 
     JSONParser parser = new JSONParser(); 
     org.json.simple.JSONArray modelArrary =(org.json.simple.JSONArray) parser.parse(result) ; 
     System.out.println(modelArrary);