Android-org.json.simple.JSONObject不能转换为org.json.JSONObject



我试图对来自JSONArray的分数进行排序,并只输出前10名。这是我的密码。

try {
JSONArray jArray = new JSONArray(result);

List<String> jsonValues = new ArrayList<String>();
for (int i = 0; i < jArray.length(); i++)
   jsonValues.add(jArray.getString(i));
Collections.sort(jsonValues);
JSONArray sortedJsonArray = new JSONArray(jsonValues);

for(int i=0;i<10;i++){
    JSONObject jObject;
    try {
        JSONParser parser = new JSONParser();
        JSONObject obj  = (JSONObject) parser.parse((String) sortedJsonArray.get(i));
        Score score =new Score();
        score.setId(obj.getInt("Id"));
        score.setPlayerId(obj.getInt("PlayerId"));
        score.setHiScore(obj.getInt("HiScore"));
        tempList.add(score);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我想将sortedJsonArray解析为JSONObject,这就是我使用org.json.simple.JSONObject的原因,但它说它不能转换为JSONObject。我尝试将org.json.simple.JSONObject数据类型用于obj,但我得到了这个错误

The method getInt(String) is undefined for the type JSONObject

我正在从网络服务器检索数据

List<Score> tempList = new ArrayList<Score>();
HttpClient client = new DefaultHttpClient(); 
HttpGet getRequest = new HttpGet(EndPoint+"/Scores");
String result="";
HttpResponse response;
try {
    response = client.execute(getRequest);
    HttpEntity entity = response.getEntity();
        if(entity!=null){
        InputStream instream = entity.getContent();
        StreamConverter sc= new StreamConverter();
        result= StreamConverter.convertStreamToString(instream);
        instream.close();
    }

编辑

我意识到我可以使用(string)将JSONArray解析为字符串,而不使用JSONParse

jObject =  new JSONObject((String) sortedJsonArray.get(i));

尝试以下解析方法:

  try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getString(KEY_SUCCESS).equals("true")) {
                preDefineAlertList = new ArrayList<HashMap<String, String>>();
                JSONArray jsonArray = jsonObject.getJSONArray("Data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    HashMap<String, String> mapNew = new HashMap<String, String>();
                    JSONObject obj = jsonArray.getJSONObject(i);
                 //   Log.d("obj", obj.toString());

                    alert_message = obj.getString(Constants.Params.MESSAGE);
                    id = obj.getString(Constants.Params.ID);

                    mapNew.put("message", message);
                    mapNew.put("id",id);

                   // Log.d("mapNew", mapNew.toString());
                    preDefinetList.add(mapNew);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

您正在使用库的JSONObject,该库使用默认JSONObject并对其进行一些操作。您使用的是simple包中该库的JSONObject。Jsonobject,您正试图将其与默认json进行比较。Jsonobject。试着看看你到底在做什么来引起它。它不仅在这种情况下对你有帮助,而且在未来也会对你有所帮助。

相关内容

最新更新