java如何在数组外提取JSON值
目前,我可以循环我的JSON数组“pos”并将其中的所有变量存储到一个数组列表中
我想做什么:
我如何轻松解析旅游&;日期哪些在“pos”数组之外
我想将这些值存储在“旅游”中&;“date”转换为两个单独的字符串变量
我的理解:json。getJSONArray()在本例中不能用作json。getJSONArray(标记消息);只拉入“pos”数组数据
JSON结构:
{
"pos": [
{
"pos": "",
"person": "",
"thur": "",
"score": "",
"round": ""
},
{
"pos": "2",
"person": "John",
"thur": "",
"score": "16",
"round": "3"
},
{
"pos": "3",
"person": "Peter Lynch",
"thur": "",
"score": "5",
"round": "2"
}
],
"tour": "Camping",
"date": "Thursday Jul 23 - Sunday Jul 26, 2015"
}
解析JSON pos数组的代码:
// ALL JSON nodes
private static final String TAG_MESSAGES = "pos";
private static final String TAG_ID = "pos";
private static final String TAG_FROM = "person";
private static final String TAG_EMAIL = "thur";
private static final String TAG_SUBJECT = "round";
private static final String TAG_DATE = "score";
// These are outside of array pos
private static final String TAG_TOUR = "tour";
private static final String TAG_TOURDATE = "date";
try {
inbox = json.getJSONArray(TAG_MESSAGES);
inbox.toString();
// looping through All messages
for (int i = 0; i < inbox.length(); i++) {
JSONObject c = inbox.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_FROM, from);
map.put(TAG_EMAIL, mailer);
map.put(TAG_DATE, date);
map.put(TAG_SUBJECT, subject);
// adding HashList to ArrayList
inboxList.add(map);
# 1 楼答案