Android JSON解析字符串无法转换为jsonobject

Posted

技术标签:

【中文标题】Android JSON解析字符串无法转换为jsonobject【英文标题】:Android JSON Parsing string cannot ne converted to jsonobject 【发布时间】:2013-10-16 18:32:03 【问题描述】:

我正在尝试从这个 URL 解析这个 JSON

https://www.google.com/calendar/feeds/sjm6mdaoghv5kipc2p80jrgsc0%40group.calendar.google.com/public/basic?alt=json

如果我尝试解析这个 url 得到 ​​p>

例外:

    10-09 12:34:31.216: W/System.err(1856): org.json.JSONException: Value Response of type java.lang.String cannot be converted to JSONObject
10-09 12:34:31.376: W/System.err(1856):     at org.json.JSON.typeMismatch(JSON.java:111)
10-09 12:34:31.391: W/System.err(1856):     at org.json.JSONObject.<init>(JSONObject.java:158)
10-09 12:34:31.546: W/System.err(1856):     at org.json.JSONObject.<init>(JSONObject.java:171)
10-09 12:34:31.701: W/System.err(1856):     at com.example.googlecalenderevents.MainActivity2$DownloadXmlTask.doInBackground(MainActivity2.java:99)
10-09 12:34:31.854: W/System.err(1856):     at com.example.googlecalenderevents.MainActivity2$DownloadXmlTask.doInBackground(MainActivity2.java:1)
10-09 12:34:32.031: W/System.err(1856):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-09 12:34:32.192: W/System.err(1856):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-09 12:34:32.201: W/System.err(1856):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-09 12:34:32.363: W/System.err(1856):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-09 12:34:32.381: W/System.err(1856):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-09 12:34:32.390: W/System.err(1856):     at java.lang.Thread.run(Thread.java:841)

这是我的代码:

public class MainActivity2 extends Activity 




    static InputStream is = null;
String json;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new DownloadXmlTask().execute();



    
    private class DownloadXmlTask extends AsyncTask<String, Void, JSONArray> 
        protected JSONArray doInBackground(String... urls) 

            // Making HTTP request
            try 
                Log.d("doInBackground", "doInBackground");

                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost("https://www.google.com/calendar/feeds/sjm6mdaoghv5kipc2p80jrgsc0%40group.calendar.google.com/public/basic?alt=json");


                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            
            catch (UnsupportedEncodingException e) 
                e.printStackTrace();
            
            catch (ClientProtocolException e) 
                e.printStackTrace();
            
            catch (IOException e) 
                e.printStackTrace();
            

            try 
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) 
                    sb.append(line + "\n");
                
                is.close();
                json = sb.toString();

            
            catch (Exception e) 
                Log.e("Buffer Error", "Error converting result " + e.toString());
            
            try 

                //JSONObject jObj = new JSONObject(json);

                JSONArray array=new JSONArray(json);




                Log.d("ffsf","fsfs");

                


            catch (JSONException e) 
                e.printStackTrace();
            
            return null;
        


        protected void onPostExecute(JSONArray result) 
            // Accessing your data



        




    

有人可以帮忙吗?

【问题讨论】:

发布服务器响应的内容 【参考方案1】:

您没有 JSONArray。 Google 返回一个 JSON 对象(请参阅它周围没有数组括号 []) 所以试试吧:

   JSONObject array=new JSONObject(json);

例如,您的 JSON 中的“链接”是一个数组

link: [
  
    rel: "alternate",
    type: "text/html",
    href: "https://www.google.com/calendar/embed?  src=sjm6mdaoghv5kipc2p80jrgsc0%40group.calendar.google.com"
  ,
  
...
  ,
 ....
],

也许从sb.append(line + "\n") 中删除+ "\n"

也许你不应该尝试https:// 试试http://

【讨论】:

如果我做 JSONObject array=new JSONObject(json);也得到同样的异常 试试 Log.d("JSON", json); 可能会从 sb.append(line + "\n"); 中删除 + "\n"; 我试过 Log.d("JSON", json);获取响应不包含任何内容类型 所以你的 json 中没有 json 字符串。

以上是关于Android JSON解析字符串无法转换为jsonobject的主要内容,如果未能解决你的问题,请参考以下文章

javascript的JSON对象

JSON 字符串上的 JSON 解析抛出“无法将对象转换为原始值”

PHP - xml 到 json 的转换:“字符串无法解析为 XML”

AsyncTask #3 解析 JSON 对象时出错。字符串无法转换为 JSONObject

PHP - xml到json转换:“无法将字符串解析为XML”

从推送通知解析时出现 SwiftyJson 解析问题