如何JSON对象转换成字符串的Android [英] How to convert json object into string in Android
本文介绍了如何JSON对象转换成字符串的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JSON格式是: -
My json format is :-
[
{
"change": 1.59,
"name": "ABC",strong text
"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
}
]
我要得到它,并把它转换成字符串,我用这种方法转换了。
I want to get it and convert it into string, I am using this method for converting it.
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);
}
但它显示是W
/System.err(867):在org.json.JSON.typeMismatch(JSON.java:111)
/System.err(867): at org.json.JSON.typeMismatch(JSON.java:111)
请帮我如何解决它。
先谢谢了。
推荐答案
请试试吧,它应该工作
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);
}
这篇关于如何JSON对象转换成字符串的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文