如何提取json数据?

时间:2013-01-03 07:20:56

标签: android

我遇到了这个麻烦?

[
{
    "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"
}

解析数据时出错org.json.JSONException:值{“incircle”:org.json.JSONObject类型的响应无法转换为JSONArray

为了提取incircle数组值,我正在编写以下代码:

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;
}

}

1 个答案:

答案 0 :(得分:1)

使用此行检索数组的元素时

JSONArray ja1= eie.getJSONArray(j);

您需要元素是自己的数组,但是,在输入中元素是对象。

由于输入只有一个数组,但你的代码假定有两个嵌套数组,你可能需要这样的东西:

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 ...