Android的JSON数组问题 [英] Android Json Array issues

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

问题描述

我知道我已经发布了很多问题,关于同一主题的,但我一直在整个未来的问题我真的不知道该如何解决。谢谢大家帮助我,我做反弹AP preciate它。我想我现在知道一些我要去为你清晰地张贴的一切错误。我试图从URL解析JSON和填充的列表,以便继承人在这种情况下,我只想标题的JSON提要

i know i have posted a lot of questions on the same topic but i keep coming across issues i don't really know how to solve. thank you to everyone that has helped me i rally do appreciate it. i think i now know the error some i'm going to post everything clearly for you. i am trying to parse json from a Url and populate a list so heres the json feed in this case i just want the title

{"code":200,"error":null,"data":{"news":[{"news_id":"8086","title":"Tickets for Player of the Year award on general sale","tagline":"Event to be held Sunday 25th March at Holiday Inn, Barnsley","content":"Tickets for the inaugural Evo-Stik NPL Player of the Year event are now on sale to the general public.\r\n\r\nPriced at \u00a335, tickets include a three course meal, plus entertainment from renowned tenor Martin Toal and guest speaker Fred Eyre.\r\n\r\nAwards at the event include the Player and Young Player of the Year for each division, as well as divisional teams of the year, the Fans\u2019 Player of the Year and a League Merit award.\r\n\r\nTo purchase your ticket, send a cheque for \u00a335 payable to \u201cNorthern Premier League\u201d to Alan Ogley, 21 Ibberson Avenue, Mapplewell, Barnsley, S75 6BJ. Please include a return address for the tickets to be sent to, and the names of those attending. \r\n\r\nFor more information, e-mail Tom Snee or contact Event organiser Alan Ogley on 07747 576 415\r\n\r\nNote: Clubs can still order tickets by e-mailing Angie Firth - simply state how many tickets you require and you will be invoiced accordingly.\r\n\r\nAddress of venue: Barnsley Road, Dodworth, Barnsley S75 3JT (just off Junction 37 of M1)","created":"2012-02-29 12:00:00","category_id":"1","img":"4539337","category":"General","fullname":"Tom Snee","user_id":"170458","image_user_id":"170458","image_file":"1330519210_0.jpg","image_width":"600","image_height":"848","sticky":"1","tagged_division_id":null,"tagged_team_id":null,"tagged_division":null,"tagged_team":null,"full_image":"http:\/\/images.pitchero.com\/up\/league-news-default-full.png","image_primary":"http:\/\/images.pitchero.com\/ui\/170458\/lnl_1330519210_0.jpg","image_secondary":"http:\/\/images.pitchero.com\/ui\/170458\/lns_1330519210_0.jpg","image_original":"http:\/\/images.pitchero.com\/ui\/170458\/1330519210_0.jpg","image_small":"http:\/\/images.pitchero.com\/ui\/170458\/sm_1330519210_0.jpg"}

我的继承人的Andr​​oid code

heres my android code

 BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();





            // Instantiate a JSON object from the request response
            JSONObject obj = new JSONObject(json);
            List<String> items = new ArrayList<String>();
            Log.i("json", "getting json");

            JSONArray jArray = obj.getJSONArray("news");


            for (int i=0; i < jArray.length(); i++)
            {    JSONObject oneObject = jArray.getJSONObject(i);
                items.add(oneObject.getString("title"));

            }

            setListAdapter ( new ArrayAdapter<String>(this, R.layout.single_item, items));
            ListView list = getListView();
            list.setTextFilterEnabled(true);


            list.setOnItemClickListener(new OnItemClickListener(){

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),1000).show();
                }



            });


        } catch(Exception e){
            // In your production code handle any errors and catch the individual exceptions
            e.printStackTrace();
        }

       }

我曾尝试在LogCat中记录它得到的结果的新闻没有价值。

I have tried logging it at LogCat gets the result no value for news.

所以我的问题是为什么呢?

so my question is why?

推荐答案

您JSON响应首先是的无效

First of your json response is invalid.

您可以测试在 JSONLint.com

二,首先你要获取数据对象作为的JSONObject ,然后你可以能够获取新闻JSONArray。

Second, first you have to fetch "data" object as a JSONObject and then you can able to fetch "news" JSONArray.

JSONObject objData = obj.getJSONObject("data");
JSONArray jArray = objData.getJSONArray("news");

这篇关于Android的JSON数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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