用android解析Json时出错

时间:2013-09-23 08:25:39

标签: android json

尝试解析json时出错。你能帮我吗?我读了json url,但是当我试图解析json时它给了我一个例外。 代码:

    public String lecturaJsonTusPerlas() {


    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://www.tvnotas.com.mx/rss/feed/tvn-horoscopo.json");
    // Depends on your web service
    httppost.setHeader("Content-type", "application/json");

    InputStream inputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
        return result;

    } catch (Exception e) { 
        Log.d("DEFEKAS","EXCEPCION");

    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }
    return result;

阅读json:

        JSONArray jsonArray2 = new JSONArray(lecturaJsonTusPerlas);

        for (int i = 0; i < jsonArray2.length(); i++) {
            JSONObject jsonObject = jsonArray2.getJSONObject(i);

        String atr1 = null;
        boolean var1 = jsonObject.getBoolean(atr1);
        String atr2 = null;
        int var2 = jsonObject.getInt(atr2);
        String atr4 = null;
        String var3 = jsonObject.getString(atr4);
        }

我认为网址是json,因为当我尝试使用Google Chrome扩展程序进行提取时,我没有任何问题。

4 个答案:

答案 0 :(得分:2)

你得到的是一个JSONObject

    JSONObject jb = new JSONObject(lecturaJsonTusPerlas());

您的json在您使用过的链接中如下所示

{ // jsonobject node
    "@attributes": {
        "version": "2.0"
    },
    "channel": {
        "title": "TV Notas",
        "link": "http://www.tvnotas.com.mx",
        "description": "TV Notas - Horoscopos",
        "pubDate": "Mon, 23 Sep 2013 2:30:12 -0500",
        "generator": "http://www.tvnotas.com.mx",
        "language": "es",
        "item": [ // json array of item's
            {
                "title": "Acuario",
                "link": "http://usa.tvnotas.com.mx/horoscopo/1-acuario/",
                "pubDate": "Mon, 23 Sep 2013 02:30:12 -0500",
                "category": "Horoscopo",
                "guid": "http://www.tvnotas.com.mx/horoscopo/1-acuario/",
                "description": "Si has sido soberbio con tus compañeros de trabajo o empleados, con familiares o amigos y no les has perdonado sus malos momentos, ahora se te presentará la oportunidad de estrechar lazos.",
                "enclosure": {
                    "@attributes": {
                        "url": "http://www.tvnotas.com.mx/advf/imagenes/2013/01/50fdbd604f653_150x118.jpg",
                        "length": "3587",
                        "type": "image/jpeg"
                    }
                },
                "elemento": "Aire",
                "planeta": "Urano",
                "signo_compatible": "Cáncer",
                "signo_enemigo": "El excéntrico",
                "numero": "25",
                "arquetipo": "El Loco Sabio",
                "arcangel": "Sakmakrel",
                "color": "Verde",
                "dia_suerte": "28"
            },
            ....

解析

try {
        JSONObject jb = new JSONObject(lecturaJsonTusPerlas());
        JSONObject job = jb.getJSONObject("channel");
        String channeltitle= job.getString("title");
        String channellink= job.getString("link");
        String channeldecription= job.getString("description");
        String channeldpubdate= job.getString("pubDate");
        String channeldgenerator = job.getString("generator");
        String channeldlanguage = job.getString("language");
        JSONArray jr = job.getJSONArray("item");

        for(int i=0;i<jr.length();i++)
        {
            JSONObject jb1 = (JSONObject) jr.get(i);
            String title = jb1.getString("title");
                            // similar for title link and others 
            JSONObject enclosure = jb1.getJSONObject("enclosure");
            JSONObject attributes= enclosure.getJSONObject("@attributes");
            String url = attributes.getString("url");
            Log.i("....",""+title);
            String elemento= jb1.getString("elemento");
            // similar for others planeta..
        }

        } catch (Exception e) {
        e.printStackTrace();
        }

答案 1 :(得分:1)

您可能会收到NullPointerException,因为这些行:

String atr1 = null;
boolean var1 = jsonObject.getBoolean(atr1);
String atr2 = null;
int var2 = jsonObject.getInt(atr2);
String atr4 = null;
String var3 = jsonObject.getString(atr4);

您正尝试使用“null”引用进行解析。 见atr1,atr2和atr4;这些字符串用“null”初始化。

答案 2 :(得分:1)

试试这个

JSONObject jObject = new JSONObject(lecturaJsonTusPerlas);

JSONArray jsonArray2 = jObject.getJSONArray("your array key");

        for (int i = 0; i < jsonArray2.length(); i++) {
            JSONObject jsonObject = jsonArray2.getJSONObject(i);

boolean var1 = jsonObject.getBoolean(atr1);
int var2 = jsonObject.getInt(atr2);
String var3 = jsonObject.getString(atr4);

}

不要将字符串值初始化为null。

答案 3 :(得分:0)

您收到此错误的原因是json元素的顶级节点是对象而不是JSON数组。

因此,要开始解析,您需要编写类似这样的内容

JSONObject jObject = new JSONObject(result);