سؤال

i am tring to convert json data from string variable like this example :

String in = "{'employees': [{'firstName':'John' , 'lastName':'Doe' },"
                + "{  'firstName' : 'Anna'  ,  'lastName' :'Smith' },"
                + "{  'firstName' : 'Peter'  ,  'lastName' : 'Jones'  }]}";
        try {
            String country = "";
            JSONArray Array = new JSONArray(in);
            for (int i = 0; i < Array.length(); i++) {
                JSONObject sys = Array.getJSONObject(i);
                country += "  " + sys.getString("firstName");
            }

            Toast.makeText(this, country, Toast.LENGTH_LONG).show();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this,    e.toString(), Toast.LENGTH_LONG).show();

        };

when i try this code i get this error :

Error parsing data org.json.JSONException: Value 0 of type java.lang.Integer cannot be   
converted to JSONObject
هل كانت مفيدة؟

المحلول

Actually your JSON is wrong formated.

Use string as below

String in = "{\"employees\": [{\"firstName\": \"John\",\"lastName\": \"Doe\"},{\"firstName\": \"Anna\",\"lastName\": \"Smith\"},{\"firstName\": \"Peter\",\"lastName\": \"Jones\"}]}";

try {
    String country = "";
    JSONObject jObj = new JSONObject(in);

        JSONArray jArray = jObj.getJSONArray("employees");
        for(int j=0; j <jArray.length(); j++){
            JSONObject sys = jArray.getJSONObject(j);
             country += "  " + sys.getString("firstName");
        }



    Toast.makeText(this, country, Toast.LENGTH_LONG).show();

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

};

نصائح أخرى

Your String in is an object with key employees and value JSONArray.

So you need to parse in as a JSONObject and get the JSONArray employees from that object.

Try this

String in = "{'employees': [{'firstName':'John' , 'lastName':'Doe' },"
            + "{  'firstName' : 'Anna'  ,  'lastName' :'Smith' },"
            + "{  'firstName' : 'Peter'  ,  'lastName' : 'Jones'  }]}";
    try {
        String country = "";
        JSONObject jObj = new JSONObject(in);

            JSONArray jArray = jObj.getJSONArray("employees");
            for(int j=0; j <jArray.length(); j++){
                JSONObject sys = jArray.getJSONObject(j);
                 country += "  " + sys.getString("firstName");
            }



        Toast.makeText(this, country, Toast.LENGTH_LONG).show();

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

    };

try below code:-

JSONObejct j = new JSONObejct(in);
JSONArray Array = j.getJSONArray("employees");

note:-

  • {} denote JSONObject. ({employe})
  • [] denote JSONArray. (employee[])

Try to replace this line :

JSONArray Array = new JSONArray(in);

With this :

JSONObject json = new JSONObject(in);
JSONArray Array = new JSONArray(json.getJSONArray("employees"));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top