0

我遇到这个麻烦了吗?

[
{
    "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: Value {"incircle": at response of type org.json.JSONObject cannot be convert to JSONArray

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

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

}

4

1 回答 1

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 ...          
于 2013-01-03T07:24:59.757 回答