本文介绍了字符串转换为JsonArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何正确此字符串转换为jsonArray?
how to properly convert this String to a jsonArray?
{
"myArray": [
{ "id": 1,
"att1": 14.2,
"att2": false },
{ "id": 2,
"att1": 13.2,
"att2": false },
{ "id": 3,
"att1": 13,
"att2": false }
]}
这是 JSONArray jArray =新JSONArray(STRING_FROM_ABOVE);
的结果 jArray.length = 1
它的我第一次取得联系JSON:)
An JSONArray jArray = new JSONArray(STRING_FROM_ABOVE);
results in jArray.length = 1
Its my first time to get in touch with json :)
推荐答案
试试这个:
JSONObject jObject = new JSONObject(STRING_FROM_ABOVE);
JSONArray jArray = jObject.getJSONArray("myArray");
在string_from_above不是一个JSON数组,这是一个JSON对象,包含一个属性(myarray的),这是一个JSON数组;)
The "string_from_above" is not a Json Array, it's a Json object, containing one attribute (myArray) which is a Json Array ;)
您可以然后执行:
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObj = jArray.getJSONObject(i);
System.out.println(i + " id : " + jObj.getInt("id"));
System.out.println(i + " att1 : " + jObj.getDouble("att1"));
System.out.println(i + " att2 : " + jObj.getBoolean("att2"));
}
这篇关于字符串转换为JsonArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!