解析数据时出错org.json.JSONException android

时间:2014-08-28 13:39:30

标签: android

我正在使用以下网址解析JSON

http://kathiyawadikhamir.com/json/post.php?postid=100

以下是代码

            @Override
      protected Void doInBackground(Void... params) {
       // TODO Auto-generated method stub

       try {
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser
          .getJSONFromUrlByGet("http://kathiyawadikhamir.com/json/post.php?postid=100");
        if (json != null) {

         title = json.getString(TAG_POST_TITLE);
         link = json.getString(TAG_POST_POST_LINK);
         imagePath = json.getString(TAG_POST_IMAGE_URL);
         content = json.getString(TAG_POST_CONTENT);


        }
       } catch (Exception e) {
        System.out.println("catch block");
        e.printStackTrace();
       }

       return null;
      }

Logcat是

     08-28 13:24:51.048: W/EGL_emulation(7548): eglSurfaceAttrib not implemented
     08-28 13:24:53.784: W/EGL_emulation(7548): eglSurfaceAttrib not implemented
     08-28 13:24:56.332: I/System.out(7548): id=2651
     08-28 13:24:56.432: W/EGL_emulation(7548): eglSurfaceAttrib not implemented
     08-28 13:24:56.460: W/EGL_emulation(7548): eglSurfaceAttrib not implemented
     08-28 13:24:57.956: E/JSON Parser(7548): Error parsing data org.json.JSONException: Value [{"content":"૨૭-૧૧-૧૯૪૦ થી ૧૭-૦૫-૨૦૦૬ (અમરેલી)\r\n\r\nરમેશ પારેખ એટલે દોમદોમ કવિતાની સાહ્યબીથી રોમરોમ છલકાતો માણસ.\r\nરમેશ પારેખ એટલે નખશિખ ગીતોના મોતીઓથી ફાટફાટ

1 个答案:

答案 0 :(得分:0)

在此处查看您的输出:http://json.parser.online.fr/

它返回JSONArray所以你得到了像

JSONArray jsonArray = jParser
          .getJSONFromUrlByGet("http://kathiyawadikhamir.com/json/post.php?postid=100");

//Now get Object from Zero position
JSONObject json = jsonArray.getJSONObject(0);

title = json.getString(TAG_POST_TITLE);
link = json.getString(TAG_POST_POST_LINK);
imagePath = json.getString(TAG_POST_IMAGE_URL);
content = json.getString(TAG_POST_CONTENT);

愿它适合你。