Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.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,我的JSON格式是: [ { "change": 1.59, "name": "ABC", "price": 10.52, "volume": 230 }, { "change": -0.05, "name": "DEF", "price": 1.06, "volume": 1040 }, { "change":

我的JSON格式是:

[
    {
        "change": 1.59,
        "name": "ABC",
        "price": 10.52,
        "volume": 230
    },
    {
        "change": -0.05,
        "name": "DEF",
        "price": 1.06,
        "volume": 1040
    },
    {
        "change": 0.01,
        "name": "GHI",
        "price": 37.17,
        "volume": 542
    }
]
我想解析它并将其转换为字符串。我正在使用此方法进行转换:

JSONObject jsonObj = new JSONObject(jsonStr);
for (int i = 0; i < jsonObj.length(); i++)
{                                              
    String change = jsonObj.getString(TAG_CHANGE);
    String name = jsonObj.getString(TAG_NAME);
    String price = jsonObj.getString(TAG_PRICE);
    String volume = jsonObj.getString(TAG_VOLUME);
    HashMap<String, String> contact = new HashMap<String, String>();

    // adding each child node to HashMap key => value
    contact.put(TAG_CHANGE, change);
    contact.put(TAG_NAME, name);
    contact.put(TAG_PRICE, price);
    contact.put(TAG_VOLUME, volume);

    // adding contact to contact list
    contactList.add(contact);
}
JSONObject jsonObj=新的JSONObject(jsonStr);
for(int i=0;ivalue
联系人。放置(标签更改,更改);
联系人:put(标签名称、姓名);
联系方式(标签价格、价格);
联系方式:放置(标签、卷、卷);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
但我有一个错误:

/System.err(867):位于org.json.json.typeMismatch(json.java:111)

如何解决此问题?

试试这个

您得到的响应是
JSONArray
,但您是作为
JSONObject

[                // this is JSONArray
      {          // this is JSONObject
改变这个

JSONObject jsonObj = new JSONObject(jsonStr);


您正在以错误的方式解析。您的json以一个包含
JsonObjects
的数组开始。这是可以识别的,因为方括号表示
JsonArray
,而大括号表示
JsonObject
。首先:

JSONArray jsonArr = new JSONArray(jsonStr);

然后遍历每个对象,在每个索引处获取
JsonObject
,并对每个键使用
getString
方法来获取值。

请尝试,它应该可以工作

JSONArray jsonObj = new JSONArray(jsonStr);

for (int i = 0; i < jsonObj.length(); i++) {
    JSONObject c = jsonObj.getJSONObject(i);
    String change = c.getString(TAG_CHANGE);
    String name = c.getString(TAG_NAME);
    String price = c.getString(TAG_PRICE);
    String volume = c.getString(TAG_VOLUME);
    HashMap < String, String > contact = new HashMap < String, String > ();
    contact.put(TAG_CHANGE, change);
    contact.put(TAG_NAME, name);
    contact.put(TAG_PRICE, price);
    contact.put(TAG_VOLUME, volume);
    contactList.add(contact);
}
JSONArray-jsonObj=新的JSONArray(jsonStr);
for(int i=0;icontact=newhashmap();
联系人。放置(标签更改,更改);
联系人:put(标签名称、姓名);
联系方式(标签价格、价格);
联系方式:放置(标签、卷、卷);
联系人列表。添加(联系人);
}
JSONArray jsonObj = new JSONArray(jsonStr);

for (int i = 0; i < jsonObj.length(); i++) {
    JSONObject c = jsonObj.getJSONObject(i);
    String change = c.getString(TAG_CHANGE);
    String name = c.getString(TAG_NAME);
    String price = c.getString(TAG_PRICE);
    String volume = c.getString(TAG_VOLUME);
    HashMap < String, String > contact = new HashMap < String, String > ();
    contact.put(TAG_CHANGE, change);
    contact.put(TAG_NAME, name);
    contact.put(TAG_PRICE, price);
    contact.put(TAG_VOLUME, volume);
    contactList.add(contact);
}