我有来自服务器的响应,这是一个有效的JSON。
{"Avg":[{"avg":3,"rcid":"ww44sd"},{"avg":4,"rcid":"ww24hj"},{"avg":3,"rcid":"ww36hd"},{"avg":4,"rcid":"ww89yx"},{"avg":3,"rcid":"ww64tf"},{"avg":4,"rcid":"ww76py"}]}
我编写了以下代码,但我不明白这不是一个JSONObject。 Logcat显示此错误
FATAL EXCEPTION: main java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
ParseCloud.callFunctionInBackground("avgfunc", params, new FunctionCallback<JSONObject>() {
public void done(JSONObject response, ParseException e) {
if (e == null) {
try {
JSONObject jsonRoot = new JSONObject(response);
JSONArray jsonarr = jsonRoot.getJSONArray("Avg");
for (int i = 0; i < jsonarr.length(); i++) {
JSONObject json = jsonarr.getJSONObject(i);
//do some processing...
}
} catch (JSONException e1) {
e1.printStackTrace();
}
在我的CloudCode中,我正在生成像这样的响应
Parse.Cloud.define("avgfunc", function(request, response) {
var idlist = request.params.someids;
var _ = require("underscore");
var newlist = {"Avg":[]}; //This is to initialize & later push
......
_.each(idlist,function(categoryid){
......
query.find({
success: function(results) {
if(results.length==0){
console.log("No row found");
}
//some more processing of data & then finally push
newlist.Avg.push({ //& this
"avg":somevalue,
"rcid":somevalue
});
}
if(counter == idlist.length)
{
response.success(newlist); //this sends the response as shown above
}
},
error: function() {
response.error("error.");
}
});
});
});
所以我想知道如何构造正确的JSONObject,以便我可以稍后在android中迭代单个项目。
编辑:堆栈跟踪
2937-2937/com.muffin.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: java.util.HashMap cannot be cast to org.json.JSONObject
at com.muffin.testapp.CollinfoActivity$2.done(CollinfoActivity.java:133)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
第133行是ParseCloud.callFunctionInBackground("getAVGrating", params, new FunctionCallback<JSONObject>() {
答案 0 :(得分:0)
json字符串似乎是合法的。这是解析它的一些代码。
我正在使用从这里下载的json-simple-1.1.1.jar: https://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar&can=2&q=
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JsonExample {
public static void main(String[] args) throws Exception {
System.out.println("Starting example...");
String str = "{\"Avg\":[{\"avg\":3,\"rcid\":\"ww44sd\"},{\"avg\":4,\"rcid\":\"ww24hj\"},{\"avg\":3,\"rcid\":\"ww36hd\"},{\"avg\":4,\"rcid\":\"ww89yx\"},{\"avg\":3,\"rcid\":\"ww64tf\"},{\"avg\":4,\"rcid\":\"ww76py\"}]}";
JSONParser parser = new JSONParser();
Object obj = parser.parse(str);
JSONObject json = (JSONObject) obj;
Object avgObj = json.get("Avg");
JSONArray avgArray = (JSONArray) avgObj;
System.out.println("JSON Strings for averages:");
for(int i=0;i<avgArray.size();i++) {
JSONObject current = (JSONObject)avgArray.get(i);
System.out.println("\t" + current);
}
System.out.println("Averages:");
for(int i=0;i<avgArray.size();i++) {
JSONObject current = (JSONObject)avgArray.get(i);
System.out.println("\tAvergage: " + current.get("avg"));
}
System.out.println("Done.");
}
}
编辑: 这是输出。
Starting example...
JSON Strings for averages:
{"avg":3,"rcid":"ww44sd"}
{"avg":4,"rcid":"ww24hj"}
{"avg":3,"rcid":"ww36hd"}
{"avg":4,"rcid":"ww89yx"}
{"avg":3,"rcid":"ww64tf"}
{"avg":4,"rcid":"ww76py"}
Averages:
Avergage: 3
Avergage: 4
Avergage: 3
Avergage: 4
Avergage: 3
Avergage: 4
Done.
答案 1 :(得分:0)
刚遇到同样的问题。您的JSON没有任何问题,它只是callFunctionInBackground
函数将所有JSONArray解析为List&lt;&gt;,将JSONObject解析为Hashmap<>
。因此,不是获取JSONObject,而是实际获得Hashmap<String, Object>
。