0

第一个 JSON

{value:[{"sk_service":"1","hour":"0","sum(transaction)":"1636"}]}

第二个 JSON

{value:[{"sk_service":"1","month":"04","sum(transaction)":"19572"}]}

然后我这样做是为了从我的 json 中知道属性

String a= JSONArray.getJSONObject(0).names().getString(0); //0
String b= JSONArray.getJSONObject(0).names().getString(1); //1

结果 :

第一个json

a = sum(transaction) //index 0.
b = hour         //index 1. 

第二个json

a = month       //index 0.
b = sum(transaction)    //index 1.

为什么第一个和第二个json的结果不一致?

4

2 回答 2

0

Is it possible that your just getting mixed up.

String a= JSONArray.getJSONObject(0).names().getString(0); 
String b= JSONArray.getJSONObject(1).names().getString(0);

getJSONObject() returns the JSON object, and in your code you seem to be using the same JSON object.

getString() returns the string matched to that index. Assuming you want the first element in each JSON, wouldn't specify 0 in both cases

于 2012-05-15T02:33:26.100 回答
0

JSON : JavaScript Object Notation - 你把它当作一个对象,你可以显式地访问属性:

这应该可以解决问题:

var data = new Array();

for(var index = 0; index < JSONArray.length; index++){
    data.push(eval("(" + JSONArray.getJSONObject(index) + ")"));
}

您现在有一个数组,可以让您显式检索数据:

data[0].value["sk_service"] will give you "1"
data[1].value["month"] will give you "04"
data[0].value["sum(transaction)"] will give you "1636"

我建议你重新考虑你的 JSON:

第二个 JSON

{value:[{"sk_service":"1","period":"month","duration":"4","transactionSum":"19572"}]}

引入 period 和 duration 属性将简化您以后重命名的代码sum(transaction),以transactionSum避免以后混淆。

于 2012-05-15T02:24:16.310 回答