-1

尝试解析 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);
        }

我认为 url 是 json,因为当我尝试使用 google chrome 扩展名进行提取时,我没有任何问题。

4

4 回答 4

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();
        }
于 2013-09-23T08:36:20.590 回答
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。

于 2013-09-23T08:35:23.093 回答
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”初始化。

于 2013-09-23T08:30:49.383 回答
0

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

所以要开始解析你需要写这样的东西

JSONObject jObject = new JSONObject(result);
于 2013-09-23T08:32:58.767 回答