【问题标题】:how to parse JSONArray in android如何在android中解析JSONArray
【发布时间】:2013-06-12 18:32:10
【问题描述】:

我想读这JSON 行但因为它以JSONArray 开头我有点困惑

 "abridged_cast": [
            {
                "name": "Jeff Bridges",
                "id": "162655890",
                "characters": [
                    "Jack Prescott"
                ]
            },
            {
                "name": "Charles Grodin",
                "id": "162662571",
                "characters": [
                    "Fred Wilson"
                ]
            },
            {
                "name": "Jessica Lange",
                "id": "162653068",
                "characters": [
                    "Dwan"
                ]
            },
            {
                "name": "John Randolph",
                "id": "162691889",
                "characters": [
                    "Capt. Ross"
                ]
            },
            {
                "name": "Rene Auberjonois",
                "id": "162718328",
                "characters": [
                    "Bagley"
                ]
            }
        ],

我只需要使用“名称”并将所有内容保存为一个字符串。 (字符串值为:Jeff Bridges、Charles Grodin、Jessica Lange、John Randolph、Rene Auberjonois)。

这是我的代码:

try {
        //JSON is the JSON code above

        JSONObject jsonResponse = new JSONObject(JSON);
        JSONArray movies = jsonResponse.getJSONArray("characters");
        String hey = movies.toString();


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

  • 你能发布完整的 json 字符串吗?
  • 您正在使用错误的名称检索数组。
  • 应该是getJSONArray("abridged_cast"),然后是for循环
  • 我不小心在我要写“名字”的代码中写了“字符”。

标签: java android arrays json


【解决方案1】:

如果您在“名称”之后,为什么您的代码 sn-p 看起来像是在尝试获取“字符”?

无论如何,这与任何其他类似列表或数组的操作没有什么不同:您只需要遍历数据集并获取您感兴趣的信息。检索所有名称应如下所示:

List<String> allNames = new ArrayList<String>();

JSONArray cast = jsonResponse.getJSONArray("abridged_cast");
for (int i=0; i<cast.length(); i++) {
    JSONObject actor = cast.getJSONObject(i);
    String name = actor.getString("name");
    allNames.add(name);
}

(直接在浏览器中输入,因此未经测试)。

【讨论】:

  • 这种格式的json呢[{"id":"1","user":"alll"},{"id":"1","user":"alll" }]
  • @numerah:你有一个没有键的数组,所以直接将json解析成JSONArray
【解决方案2】:

getJSONArray(attrname) 将从给定属性名称的对象中获取一个数组 在你的情况下发生的事情是

{"abridged_cast":["name": blah...]}
^ its trying to search for a value "characters"

但你需要进入数组,然后搜索“字符”

试试这个

String json="{'abridged_cast':[{'name':'JeffBridges','id':'162655890','characters':['JackPrescott']},{'name':'CharlesGrodin','id':'162662571','characters':['FredWilson']},{'name':'JessicaLange','id':'162653068','characters':['Dwan']},{'name':'JohnRandolph','id':'162691889','characters':['Capt.Ross']},{'name':'ReneAuberjonois','id':'162718328','characters':['Bagley']}]}";

    JSONObject jsonResponse;
    try {
        ArrayList<String> temp = new ArrayList<String>();
        jsonResponse = new JSONObject(json);
        JSONArray movies = jsonResponse.getJSONArray("abridged_cast");
        for(int i=0;i<movies.length();i++){
            JSONObject movie = movies.getJSONObject(i);
            JSONArray characters = movie.getJSONArray("characters");
            for(int j=0;j<characters.length();j++){
                temp.add(characters.getString(j));
            }
        }
        Toast.makeText(this, "Json: "+temp, Toast.LENGTH_LONG).show();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

检查过:)

【讨论】:

    【解决方案3】:

    这是一个更好的方法。希望这会有所帮助

    protected void onPostExecute(String result) {
                    Log.v(TAG + " result);
    
    
                    if (!result.equals("")) {
    
                        // Set up variables for API Call
                        ArrayList<String> list = new ArrayList<String>();
    
                        try {
                            JSONArray jsonArray = new JSONArray(result);
    
                            for (int i = 0; i < jsonArray.length(); i++) {
    
                                list.add(jsonArray.get(i).toString());
    
                            }//end for
                        } catch (JSONException e) {
                            Log.e(TAG, "onPostExecute > Try > JSONException => " + e);
                            e.printStackTrace();
                        }
    
    
                        adapter = new ArrayAdapter<String>(ListViewData.this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
                        listView.setAdapter(adapter);
                        listView.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
                                // ListView Clicked item index
                                int itemPosition = position;
    
                                // ListView Clicked item value
                                String itemValue = (String) listView.getItemAtPosition(position);
    
                                // Show Alert
                                Toast.makeText( ListViewData.this, "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG).show();
                            }
                        });
    
                        adapter.notifyDataSetChanged();
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 2016-08-17
      相关资源
      最近更新 更多