我无法使用JSON解析检索数据

时间:2011-12-29 11:36:55

标签: android

{
    "user": {
        "name": ["bineesh", "Administrator", "binu", "binu", "bijith", "prem"]
    },
    "email": ["bineesh256@gmail.com", "erpadmin@gmail.com", "binu245@gmail.com", "binu245@gmail.com", "bijith256@gmail.com", "toast@gmail.com"],
    "phone": ["7293553814", "12345", "0", "0", "0", "9046567239"]
}

我无法解析这个回复:

Object(result);

// JSONObject  jObject;
// jObject= new JSONObject(result);

JSONArray ja = new JSONArray(result);
for (int i = 0; i < ja.length(); i++) {
    JSONObject jo = (JSONObject) ja.get(i);
    System.out.println(jo.getString("name"));
}

如何在列表视图中看到此响应?

3 个答案:

答案 0 :(得分:1)

示例代码:

public class HomeActivity extends ListActivity {

    /** Called when the activity is first created. */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.fetchTwitterPublicTimeline()));
    }

    public ArrayList<String> fetchTwitterPublicTimeline()
    {
        ArrayList<String> listItems = new ArrayList<String>();

        try {
            URL twitter = new URL(
                    "http://twitter.com/statuses/public_timeline.json");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("text"));
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
}

答案 1 :(得分:0)

您可以使用以下方法解析JSON文件

                JSONObject obj = new JSONObject(result);

                JSONObject objUser = obj.getJSONObject("user");
                JSONArray arrUser = objUser.getJSONArray("name");
                for(int i=0;i<arrUser.length();i++)
                {
                    String name = arrUser.getString(i);
                }

                JSONArray arrEmail = objUser.getJSONArray("email");
                for(int i=0;i<arrEmail.length();i++)
                {
                    String email = arrEmail.getString(i);
                }

                JSONArray arrPhone = objUser.getJSONArray("phone");
                for(int i=0;i<arrPhone.length();i++)
                {
                    String phone = arrPhone.getString(i);
                }

答案 2 :(得分:0)

示例代码:

public void parse(String s){        
    try {
        JSONObject jsonObject  = new JSONObject(s);
        JSONObject namejObj  = jsonObject.getJSONObject("user");
        JSONArray nameArray = namejObj.getJSONArray("name");
        for(int i =0;i<nameArray.length();i++){
            Log.i("System out","name : "+nameArray.getString(i).toString());
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}