简体   繁体   中英

How to extract json data?

I am getting this trouble?

[
{
    "username": "apap",
    "status": "",
    "log": "",
    "email": "apap@gmail.com",
    "lat": "",
    "dob": "5-11-1986"
},
{
    "username": "apapp",
    "status": "",
    "log": "",
    "email": "apapp@gmail.com",
    "lat": "",
    "dob": "5-11-1986"
}
],
"returncode": "0"
}

Error parsing data org.json.JSONException: Value {"incircle": at response of type org.json.JSONObject cannot be converted to JSONArray

For extracting incircle array value,I am writing following code:

public class MainActivity extends Activity {
TextView username;
TextView email;
TextView dob;
TextView lat;
TextView log;
ListView listView;
ArrayList<HashMap<String,String>> jsonlist;
JsonAdapter objAdapter;
Context ctx;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listView = (ListView) findViewById(R.id.listView);
    jsonlist = new ArrayList<HashMap<String,String>>();
    JSONObject json = JSONfunctions
            .getJSONfromURL("http://www.railsboxtech.com/flirtalert/alluser.php");

    try {

        JSONArray flirt = json.getJSONArray("response");
        Toast.makeText(MainActivity.this, "namr", Toast.LENGTH_LONG).show();
        for (int i = 0; i < flirt.length(); i++) {

            JSONObject ei = flirt.getJSONObject(i);
            JSONArray eie = ei.getJSONArray("incircle");
            for (int j = 0; j < eie.length(); j++) {
                JSONObject e = eie.getJSONObject(j);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("User",e.getString("username"));
                map.put("Email",e.getString("email"));
                map.put("dob",e.getString("dob"));
                map.put("lat",e.getString("lat"));
                map.put("log",e.getString("log"));
                jsonlist.add(map);

            }

        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    ListAdapter adapter = new SimpleAdapter(this, jsonlist, R.layout.user_list,
            new String[] { "User", "Email","dob","lat","log"}, new int[] {
                    R.id.username, R.id.email, R.id.dob, R.id.lat, R.id.log});
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

When you retrieve the elements of the array with this line

JSONArray ja1= eie.getJSONArray(j);

You require the elements to be arrays themselfs, however, in your input the elements are objects.

Since the input has only one array but your code assumes two nested arrays, you probably want something like:

JSONObject json = JSONfunctions
        .getJSONfromURL("http://www.railsboxtech.com/flirtalert/alluser.php");

try {
    JSONObject ei = json.getJSONObject("response");
    JSONArray eie = ei.getJSONArray("incircle");
    for (int j = 0; j < eie.length(); j++) {
        JSONObject e = eie.getJSONObject(j);
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("User",e.getString("username"));
        map.put("Email",e.getString("email"));
        map.put("dob",e.getString("dob"));
        map.put("lat",e.getString("lat"));
        map.put("log",e.getString("log"));
       jsonlist.add(map);
   }
} catch ...          

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM