原始Json数据
[
{
"worker.1":{
"last_share":1443639029,
"score":"3722204.62578",
"alive":true,
"shares":1124332,
"hashrate":1047253
},
"worker.2":{
"last_share":1443639029,
"score":"3794755.69049",
"alive":true,
"shares":1069332,
"hashrate":1012070
},
"worker.3":{
"last_share":1440778690,
"score":"0.0",
"alive":false,
"shares":0,
"hashrate":0
},
"worker.4":{
"last_share":1443638222,
"score":"940190.67723",
"alive":true,
"shares":449932,
"hashrate":404772
}
"worker.nth":{
"last_share":1443638222,
"score":"940190.67723",
"alive":true,
"shares":449932,
"hashrate":404772
}
}
]
从主要问题我能够缩小结果范围并设法添加Square括号。现在我上面有一个Json数组,所以有人可以告诉我如何处理这个Json数组。另请注意,最终可能会有1000名工人。
请帮助我在这个问题上突破了2天 另请注意,实际的Json数据很复杂且有1000个条目, 包含的json数据只是一个概述。
编辑:我用来处理上述Json的代码
try{
JSONArray jArray = new JSONArray(jsonObj.getJSONObject("workers"));
if(jArray.length() == 0){
Log.d("Json Err", "JSON string has no entries");
return null;
}
JSONObject jObject = jArray.getJSONObject(0);
// To get one specific element:
JSONObject worker1 = jObject.getJSONObject("worker.1");
JSONObject worker2 = jObject.getJSONObject("worker.2");
// Iterate over all elements
Iterator<?> keys = jObject.keys();
while( keys.hasNext() ) {
String key = (String)keys.next(); // "worker.1", etc
if ( jObject.get(key) instanceof JSONObject ) {
JSONObject entry = jObject.getJSONObject(key);
// Do something with your entry here like:
String score = entry.optString("score", "");
Boolean alive = entry.optBoolean("alive", false);
Integer shares = entry.optInt("shares", 0);
Log.d("Json Success", score );
}
}
} catch (JSONException e){
Log.e("Json Err", "Failure converting JSON String", e);
}
我收到错误:
E2570(下午5:10): org.json.JSONException:Value {“worker.1”:{“last_share”:1443694390,“score”:“15.6018529132”,“alive”:true,“shares”:59880,“hashrate”:0},“worker 0.2 “:{” last_share “:1443694180,” 分数 “:” 2.97304689833" , “活着”:真正的 “股”:2048, “hashrate”:0}, “worker.3”:{ “last_share”:1440778690 , “分数”: “0.0”, “活的”:假, “股份”:0, “hashrate”:0}, “ivonme.ant4”:{ “last_share”:1443701343, “得分”: “8688.78118933”,”活着 “:真正的” 股 “:203088,” hashrate“:78633}} 类型org.json.JSONObject无法转换为JSONArray 在org.json.JSON.typeMismatch(JSON.java) 在org.json.JSONArray。(JSONArray.java) 在org.json.JSONArray。(JSONArray.java)
答案 0 :(得分:1)
所以你有......
{ //JSONObject
"worker.1":{
"last_share":1443639029,
"score":"3722204.62578",
"alive":true,
"shares":1124332,
"hashrate":1047253
},
"worker.2":{
"last_share":1443639029,
"score":"3794755.69049",
"alive":true,
"shares":1069332,
"hashrate":1012070
} //,...
}
为了得到你所有的工人......
try{
JSONObject jObject = new JSONObject(yourStringFromYourQuestion);
// To get one specific element:
JSONObject worker1 = jObject.getJSONObject("worker.1");
JSONObject worker2 = jObject.getJSONObject("worker.2");
// Iterate over all elements
Iterator<?> keys = jObject.keys();
while( keys.hasNext() ) {
String key = (String)keys.next(); // "worker.1", etc
if ( jObject.get(key) instanceof JSONObject ) {
JSONObject entry = jObject.getJSONObject(key);
// Do something with your entry here like:
String score = entry.optString("score", "");
Boolean alive = entry.optBoolean("alive", false);
Integer shares = entry.optInteger("shares", 0);
}
}
} catch (JSONException e){
Log.e(LOG_TAG, "Failure converting JSON String", e);
}
答案 1 :(得分:0)
试试这个:
5
而不是
JSONArray jArray=jsonObj.getJSONArray("workers");
错误是因为你将json数组作为对象。我想是的。