[英]Not able to convert String to JSONObject
I am getting an inputstream and converting it to String
and then to JSONObject
我正在获取一个输入流并将其转换为
String
然后转换为JSONObject
Below is snippet for converting inputstream to String and then JSONObject.下面是将输入流转换为字符串,然后是 JSONObject 的片段。
But after converting to json object(line 6) I am getting only the first object
instead of all the objects
但是在转换为 json 对象(第 6 行)后,我只得到第一个
object
而不是所有objects
Can you please let me know why I am getting only one object instead of all the n 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
After converting it to String it look like this将其转换为 String 后,它看起来像这样
Result -1结果 -1
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
}, {...// n times}
Result-2 -only first object结果 2 - 仅第一个对象
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
}
Thanks and please bear my ignorance as I am completly new in java谢谢,请容忍我的无知,因为我完全是 Java 新手
Because you json is not valid .There is a comma between JSONObject
.因为你的 json 无效。
JSONObject
之间有一个逗号。
Change to this .改成这个。
{
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
},
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
}
...
}
or或者
[
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
},
{
"test_expr":"",
"val_expr":"someVale",
"val_advanced":true,
"machine_val":null
}
...
]
The source of JSONObject
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 a JSON-encoded string containing an object(like {}).所以一个包含对象的 JSON 编码字符串(如 {})。
// make sure that you result contain {}
result = "{your data here}"
JSONObject json_data = new JSONObject(result);
And if you use JSONArray
,you should contain [] in your JSON
如果你使用
JSONArray
,你应该在你的JSON
包含 []
result = "[json data]";
JSONArray jsonArray = new JSONArray(result);
Well, concatenation of multiple jsons is not a valid json.好吧,多个 json 的串联不是有效的 json。 Your parsing library should have rejected such an input, but it seems that it just stopped at the end of the valid object.
您的解析库应该拒绝这样的输入,但它似乎只是在有效对象的末尾停止。
You can wrap the list it into an array: [{...},{...},{...}].您可以将列表包装成一个数组:[{...},{...},{...}]。 Then the parser will be able to correctly interpret it as an array.
然后解析器将能够正确地将其解释为数组。
I guess you are getting JSONArray object instead of JSONObject.我猜你得到的是 JSONArray 对象而不是 JSONObject。 Why do you need to get sub string of the result?
为什么需要获取结果的子字符串? Json array can start with [.
Json 数组可以以 [. See the difference between JSONObject and JSONArray.
查看 JSONObject 和 JSONArray 之间的区别。 Difference between JSONObject and JSONArray
JSONObject 和 JSONArray 的区别
Thanks to Sotirios Delimanolis.感谢 Sotirios Delimanolis。
I am able to resolve the problem by using JSONParser我可以通过使用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.