如何从一个JSON数组访问invidual JSON对象? [英] How to access invidual json objects from a json array?

查看:211
本文介绍了如何从一个JSON数组访问invidual JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的回应,我要显示所有 eqid 的在invidual 的TextView 的,但我不知道怎么才达到这一点,
下面是我的代码片段code和响应从服务器获取和显示在TextView中的数据。结果
谁能帮我这个?

  {
  地震:[
    {
      eqid:c0001xgp,    },
    {
      eqid:c000905e,    },
    {
      eqid:2007hear,    },
    {
      eqid:c00090da,    },
    {
      eqid:2007aqbk,    },
    {
      eqid:2007hec6,    },
    {
      eqid:a00043nx,    },
    {
      eqid:2010utc5,    },  ]
}

code

 的ServiceHandler SH =新的ServiceHandler();
    jsonStr = sh.makeServiceCall(USER_URL,ServiceHandler.GET);
    Log.d(回应:,>中+ jsonStr);    尝试{
        JSONObject的JSON =新的JSONObject(jsonStr);
        JSONArray项目= json.getJSONArray(产品);        parent_layout =(RelativeLayout的)findViewById(R.id.rl);
        的for(int i = 0; I< items.length();我++){            TextView中的TextView =新的TextView(本);
            textView.setText(items.getJSONObject(I).getString(USER_NAME));            parent_layout.addView(TextView的);
        }


解决方案

\r
\r

{尝试\r
    JSONObject的JSON =新的JSONObject(jsonstr);\r
    JSONArray项= json.getJSONArray(地震);\r
\r
    的for(int i = 0; I< items.length();我++){\r
        TextView的T =新的TextView(背景);\r
        t.se​​tText(items.getJSONObject(ⅰ).getString('eqid'));\r
        ParentLayout.addView(T);\r
    }\r
}赶上(JSONException E){\r
    e.printStacktrace();\r
}

\r

\r
\r

I have the following response, and I want to display all eqid's in invidual TextView's, but I don't know how to achive this, below is my snippet code and response to get data from the server and display in textview.
Can anyone help me with this?

{
  "earthquakes":[
    {
      "eqid":"c0001xgp",

    },
    {
      "eqid":"c000905e",

    },
    {
      "eqid":"2007hear",

    },
    {
      "eqid":"c00090da",

    },
    {
      "eqid":"2007aqbk",

    },
    {
      "eqid":"2007hec6",

    },
    {
      "eqid":"a00043nx",

    },
    {
      "eqid":"2010utc5",

    },

  ]
}

Code

   ServiceHandler sh = new ServiceHandler();
    jsonStr = sh.makeServiceCall(USER_URL, ServiceHandler.GET);
    Log.d("Response: ", "> " + jsonStr);

    try{
        JSONObject json = new JSONObject(jsonStr);
        JSONArray items = json.getJSONArray("product");

        parent_layout = (RelativeLayout)findViewById(R.id.rl);
        for(int i =0; i<items.length(); i++){



            TextView textView = new TextView(this);
            textView.setText(items.getJSONObject(i).getString(USER_NAME));

            parent_layout.addView(textView);
        }

解决方案

try{
    JSONObject json = new JSONObject(jsonstr);
    JSONArray items = json.getJSONArray("earthquakes");

    for(int i =0; i<items.length(); i++){
        TextView t=new TextView(context);
        t.setText(items.getJSONObject(i).getString('eqid'));
        ParentLayout.addView(t);
    }
}catch(JSONException e){
    e.printStacktrace();
}

这篇关于如何从一个JSON数组访问invidual JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆