当前位置:  开发笔记 > 前端 > 正文

Android以及如何将字符串对象更改为json对象

如何解决《Android以及如何将字符串对象更改为json对象》经验,为你挑选了1个好方法。

我试图从我的服务器使用restful服务获得响应,我以json格式返回,更改为字符串变量然后我尝试从字符串变量更改为json对象,但我收到错误这里是我的类在我的类是我的堆栈跟踪然后我的字符串变量之后,当我尝试从字符串转换为json对象时发生错误,感谢您的帮助:

private class DownloadWebPageTask extends AsyncTask {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                //JSONObject json = new JSONObject(response);
                JSONObject data = new JSONObject(response).getJSONObject("type");

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return response;
        }

11-14 09:53:26.192:W/System.err(560):org.json.JSONException:Value [{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/ watch/humvideo:0373889 /","label":"Harry Potter und der Orden des Phonix","videoinfo":{"id":"humvideo:0373889","title":"Harry Potter und der Orden des Phonix" ,"身高":480,"覆盖":"BYU","身份":"允许","主题":"哈利波特,魔法","描述":"Lord Voldemort ist zuruckgekehrt,doch das Zaubereiministerium tut alles,嗯diese Tatsache der Geme ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2007-04-11 "}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0330373/","label":"Harry Potter und der Feuerkelch","videoinfo ":{"id":"humvideo:0330373","title":"Harry Potter und der Feuerkelch","height":480,"coverage":"BYU","status":"允许","主题" :"哈利波特,魔术","描述":"Das grosse Abenteuer开始,als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ......","wi DTH ":716," 权利 ":" 共享 " "语言":[ "德"], "runningtime":7200, "日期": "2005-11-18"}},{ "类型":" HumVideoMovingImage ","location":"http://hummedia.byu.edu/watch/humvideo:0304141/","label":"Harry Potter und der Gefangene von Askaban","videoinfo":{"id":"humvideo :0304141","title":"Harry Potter und der Gefangene von Askaban","height":480,"coverage":"BYU","status":"允许","主题":"哈利波特,魔法" ,"描述":"Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt,kehrt Harry nach Hogwarts zuruck,...","width":716,"rights":"shared","language":["de"] "runningtime":7200, "日期": "2003-06-04"}},{ "类型": "HumVideoMovingImage", "位置":"http://hummedia.byu.edu/watch/humvideo:0295297 /","标签":"Harry Potter und die Kammer Desschreckens","videoinfo":{"id":"humvideo:0295297","title":"Harry Potter und die Kammer Desschreckens","height":480, "报道":"BYU","状态":"允许","主题":"哈利波特,魔术","描述":"Fliegende Autos,gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - 所以刚开始......","宽度":716,"权利":"共享","语言":["de"],"runningtime":7200,"date":"2002-11- 15"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0241527/","label":"Harry Potter und der Stein der Weisen" ,"videoinfo":{"id":"humvideo:0241527","title":"Harry Potter und der Stein der Weisen","height":480,"coverage":"BYU","status":"允许",""主题":"哈利波特,魔术","描述":"In der zauberhaften Verfilmung JK Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ......","width":716,"rights":"shared ",""语言":["de"],"runningtime":7200,"date":"2001-11-16"}}]类型org.json.JSONArray无法转换为JSONObject 11-14 09:53 :26.232:W/System.err(560):at org.json.JSON.typeMismatch(JSON.java:111)11-14 09:53:26.242:W/System.err(560):at org.json. JSONObject.(JSONObject.java:158)11-14 09:53:26.252:W/System.err(560):at org.json.JSONObject.(JSONObject.java:171)11-14 09:53:26.262: W/System.err(560):at de.vogella.android.asynctask.ReadWebpageAsyncTask $ DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:56)11-14 09:53:26.332:W/System.err(560):at de.vogella.android.asynctask.ReadWebpageAsyncTask $ DownloadWebPageTask .doInBackground(ReadWebpageAsyncTask.java:1)11-14 09:53:26.352:W/System.err(560):at android.os.AsyncTask $ 2.call(AsyncTask.java:264)11-14 09:53: 26.372:W/System.err(560):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305)11-14 09:53:26.382:W/System.err(560):at java .util.concurrent.FutureTask.run(FutureTask.java:137)11-14 09:53:26.392:W/System.err(560):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:208 )11-14 09:53:26.402:W/System.err(560):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)11-14 09:53:26.412:W/System.err (560):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569)11-14 09:53:26.422:W/System.err(560):at java.lang.Thread.run( Thread.java:856)11- 14 09:53:34.635:D/dalvikvm(169):GC_CONCURRENT释放396K,7%免费7350K/7879K,暂停21ms + 9ms

字符串变量:

[{"type":"HumVideoMovingImage",
"videoinfo":{"status":"允许",
"描述":"Lord Voldemort ist zuruckgekehrt,doch das Zaubereiministerium tut alles,um diese Tatsache der Geme ......",
"身高":480,"报道":"BYU","日期":"2007-04-11","id":"humvideo:0373889",
"主题":"哈利波特,魔术","语言": ["de"],
"rights":"shared","title":"Harry Potter und der Orden des Phonix","runningtime":7200,"width":716},"location":"http:// hummedia.byu.edu/watch/humvideo:0373889/","label":"Harry Potter und der Orden des Phonix"},{
"type":"HumVideoMovingImage","videoinfo":{
"status":"allowed" ,"描述":"Das grosse Abenteuer开始,als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ......","身高":480,"覆盖":"BYU","日期":"2005-11- 18","id":"humvideo:0330373","subject":"哈利波特,魔术",
"语言":["de"],"权利":"共享",
"标题":"Harry Potter und der Feuerkelch",
" runningtime" :7200,"width":716},
"location":"http:// h ummedia.byu.edu/watch/humvideo:0330373/","label":"Harry
Potter und der Feuerkelch"},{"type":"HumVideoMovingImage","videoinfo":{
"status":"allowed","描述":"Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt,kehrt Harry nach Hogwarts zuruck,...",""height":480,
"coverage":"BYU","date":"2003-06-04" ,
"id":"humvideo:0304141","subject":"Harry Potter,magic","language":["de"],"rights":"shared","title":"Harry Potter und der Gefangene von Askaban","runningtime":7200,"width":716
},"location":"http://hummedia.byu.edu/watch/humvideo:0304141/","label":"Harry Potter und der Gefangene von Askaban"},
{"type":"HumVideoMovingImage","videoinfo":{"status":"允许","描述":"Fliegende Autos,gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - so beginnt fu ... ",""身高":480,"覆盖":"BYU","日期":"2002-11-15","id":"humvideo:0295297","主题":"哈利波特,魔术",
"语言":["de"],"权利":"共享",
"标题 ":"Harry Potter und die Kammer Desschreckens",
" runningtime" :7200,"width":716},
"location":"http://hummedia.byu.edu/watch/humvideo:0295297/",
"label" :"Harry Potter und die Kammer Desschreckens"},
{"type":"HumVideoMovingImage","videoinfo":{"status":"allowed","description":"In der zauberhaften Verfilmung JK Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ...","height":480,"coverage":"BYU","date":"2001-11-16","id":"humvideo:0241527","subject":"Harry Potter,magic",
"language":["de"],"rights":"shared",
"title":"Harry Potter und der Stein der Weisen",
"runningtime":7200,"width":716},
"location":"http://hummedia.byu.edu/watch/humvideo:0241527/",
"label":"Harry Potter und der Stein der Weisen"}

    ]

通过尝试使用以下代码:JSONObject jObject = new JSONObject(response);

我仍然得到一个错误,这是堆栈跟踪:

11-14 10:03:02.713:W/System.err(622):org.json.JSONException:Value [{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/ watch/humvideo:0373889 /","label":"Harry Potter und der Orden des Phonix","videoinfo":{"id":"humvideo:0373889","title":"Harry Potter und der Orden des Phonix" ,"身高":480,"覆盖":"BYU","身份":"允许","主题":"哈利波特,魔法","描述":"Lord Voldemort ist zuruckgekehrt,doch das Zaubereiministerium tut alles,嗯diese Tatsache der Geme ...","width":716,"rights":"shared","language":["de"],"runningtime":7200,"date":"2007-04-11 "}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0330373/","label":"Harry Potter und der Feuerkelch","videoinfo ":{"id":"humvideo:0330373","title":"Harry Potter und der Feuerkelch","height":480,"coverage":"BYU","status":"允许","主题" :"哈利波特,魔术","描述":"Das grosse Abenteuer开始,als der Feuerkelch Harry Potters Namen freigibt und Harry damit Teilne ......","wi DTH ":716," 权利 ":" 共享 " "语言":[ "德"], "runningtime":7200, "日期": "2005-11-18"}},{ "类型":" HumVideoMovingImage ","location":"http://hummedia.byu.edu/watch/humvideo:0304141/","label":"Harry Potter und der Gefangene von Askaban","videoinfo":{"id":"humvideo :0304141","title":"Harry Potter und der Gefangene von Askaban","height":480,"coverage":"BYU","status":"允许","主题":"哈利波特,魔法" ,"描述":"Wahrend die abscheuliche Tante Magda uber den Nachthimmel schwebt,kehrt Harry nach Hogwarts zuruck,...","width":716,"rights":"shared","language":["de"] "runningtime":7200, "日期": "2003-06-04"}},{ "类型": "HumVideoMovingImage", "位置":"http://hummedia.byu.edu/watch/humvideo:0295297 /","标签":"Harry Potter und die Kammer Desschreckens","videoinfo":{"id":"humvideo:0295297","title":"Harry Potter und die Kammer Desschreckens","height":480, "报道":"BYU","状态":"允许","主题":"哈利波特,魔术","描述":"Fliegende Autos,gemeingefahrliche Baume und die Warnung eines mysteriosen Hauselfen - 所以刚开始......","宽度":716,"权利":"共享","语言":["de"],"runningtime":7200,"date":"2002-11- 15"}},{"type":"HumVideoMovingImage","location":"http://hummedia.byu.edu/watch/humvideo:0241527/","label":"Harry Potter und der Stein der Weisen" ,"videoinfo":{"id":"humvideo:0241527","title":"Harry Potter und der Stein der Weisen","height":480,"coverage":"BYU","status":"允许",""主题":"哈利波特,魔术","描述":"In der zauberhaften Verfilmung JK Rowlings Weltbestseller erfahrt Harry Potter an seinem 11. Gebur ......","width":716,"rights":"shared ",""语言":["de"],"runningtime":7200,"date":"2001-11-16"}}]类型org.json.JSONArray无法转换为JSONObject 11-14 10:03 :02.732:W/System.err(622):at org.json.JSON.typeMismatch(JSON.java:111)11-14 10:03:02.752:W/System.err(622):at org.json. JSONObject.(JSONObject.java:158)11-14 10:03:02.762:W/System.err(622):at org.json.JSONObject.(JSONObject.java:171)11-14 10:03:02.822: W/System.err(622):at de.vogella.android.asynctask.ReadWebpageAsyncTask $ DownloadWebPageTask.doInBackground(ReadWebpageAsyncTask.java:56)11-14 10:03:02.842:W/System.err(622):at de.vogella.android.asynctask.ReadWebpageAsyncTask $ DownloadWebPageTask .doInBackground(ReadWebpageAsyncTask.java:1)11-14 10:03:02.852:W/System.err(622):at android.os.AsyncTask $ 2.call(AsyncTask.java:264)11-14 10:03: 02.872:W/System.err(622):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305)11-14 10:03:02.892:W/System.err(622):at java .util.concurrent.FutureTask.run(FutureTask.java:137)11-14 10:03:02.902:W/System.err(622):at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:208 )11-14 10:03:02.912:W/System.err(622):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)11-14 10:03:02.922:W/System.err (622):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569)11-14 10:03:02.942:W/System.err(622):at java.lang.Thread.run( Thread.java:856)

谢谢.



1> Maneesh..:

你只需要使用下面的代码

import org.json.JSONObject;

JSONObject jObject = new JSONObject(response);

休息时,您可以以相同的方式处理

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有