将字符串转换成JSONObject的 [英] Converting a String to JSONObject

查看:146
本文介绍了将字符串转换成JSONObject的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是要得到正确的进去。我得到了LogCat中的错误,说我的JSON字符串不能转换成JSONOject,我真的不知道该如何工作我解决这个错误,所以我只是希望有人有更好的知识比我可以给我的建议如何我可以参加到这个问题。

logcat的:

  08-02 00:12:28.485:E / JSON解析器(778):错误分析数据org.json.JSONException:值< java.lang.String类型的文档类型!不能转换到的JSONObject

JSONParser类

 公共类JSONParser {    InputStream为;
    的JSONObject jObj;
    JSON字符串=;
    公众的EditText等;    公共JSONParser(){
    }    公众的JSONObject readJSONFeed(字符串URL){        尝试{
        HttpClient的客户端=新DefaultHttpClient();
        HttpPost要求=新HttpPost(URL);
        //request.setURI(website);
        尝试{
            HTT presponse响应= client.execute(请求);
        HttpEntity httpEntity = response.getEntity();
        是= httpEntity.getContent();        }赶上(ClientProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\\ n);
            }
            is.close();
            JSON = sb.toString();
        }赶上(例外五){
            Log.e(缓冲区错误,错误转换结果+ e.toString());
        }        尝试{
            jObj =新的JSONObject(JSON);
        }赶上(JSONException E){
           Log.e(JSON解析器,错误分析数据+ e.toString());
        }        返回jObj;        }最后{}    } {
    }}

这是导致出现此错误的方法:

  {尝试
        jObj =新的JSONObject(JSON);
    }赶上(JSONException E){
       Log.e(JSON解析器,错误分析数据+ e.toString());
    }

我印我的JSON字符串:

  20 08-02:02:27.535:D / JSON字符串(795):< D​​OCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 // EN http://www.w3.org/TR/html4/strict.dtd\">
08-02 20:02:27.535:D / JSON字符串(795):其中,HTML>< HEAD><标题>雅虎! - 404未找​​到< /标题><风格>
08-02 20:02:27.535:D / JSON字符串(795):/ * NN4隐藏* /
08-02 20:02:27.535:D / JSON字符串(795):/ * / * /
08-02 20:02:27.535:D / JSON字符串(795):身体{字体:小/ 1.2em宋体,黑体,清洁,无衬线;字体:X-小;文本对齐:中心;} {表字体大小:继承;字体:X-小;}
08-02 20:02:27.535:D / JSON字符串(795):HTML>机身{字体:83%/ 1.2em宋体,黑体,清洁,无衬线;} {输入字体大小:100%; vertical-对齐:中间;} p,形式{保证金:0;填充:0;}
08-02 20:02:27.535:D / JSON字符串(795):p {填充底:6像素;保证金底:10px的;}#{DOC宽度:48.5em;保证金:0汽车;边界:1px的固体# FFF;文本对齐:中心;}#{ygma文本对齐:权利;保证金底:53px}
08-02 20:02:27.535:D / JSON字符串(795):#ygma IMG {浮动:左;}#DIV ygma {下边框:1px的固体#CCC;填充底:8像素;保证金左:152px ;}#{BD明确:两者;文本对齐:左;宽度:75%;保证金:0自动为20px;}
08-02 20:02:27.535:D / JSON字符串(795):H1 {字体大小:135%;文本对齐:中心;保证金:0 0 15px的;} {传说显示:无;} {字段集边框: 0固体#FFF;填充:0 .8em .8em 4.5em;}
08-02 20:02:27.535:D / JSON字符串(795):表格{位置:亲属;背景:#eee;保证金底:15px的;边界:1px的固体#CCC;边框宽度:1px的0;}
08-02 20:02:27.535:D / JSON字符串(795):#{S1P宽度:15em;保证金右:.1em;}
08-02 20:02:27.535:D / JSON字符串(795):跨度形式{位置:绝对;左:70%;顶部:.8em;}形成{字体:78%/ 1.2em Arial字体;显示:块;填充左:.8em;空格:NOWRAP;背景:网址(http://l.yimg.com/a/i/s/bullet.gif)不重复左侧居中;}
08-02 20:02:27.535:D / JSON字符串(795):形成.SEP {显示:无;} {更多的text-align:中心;}#{英尺填充顶:10px的;边框顶部:1px的固体#999;}#英尺p {文本对齐:中心;字体:78%宋体;}
08-02 20:02:27.535:D / JSON字符串(795):/ *结束NN4隐藏* /
08-02 20:02:27.535:D / JSON字符串(795):其中,/风格与GT;< /头>
08-02 20:02:27.535:D / JSON字符串(795):其中,身体GT;< D​​IV ID =DOC>
08-02 20:02:27.535:D / JSON字符串(795):其中,股利的id =ygma&GT;&下; A HREF =htt​​p://us.rd.yahoo.com/404/*http: //www.yahoo.com\"><img
08-02 20:02:27.535:D / JSON字符串(795):SRC = HTTP://l.yimg.com/a/i/yahoo.gif
08-02 20:02:27.535:D / JSON字符串(795):宽= 147 HEIGHT = 31 BORDER = 0 ALT =!雅虎&GT;&LT; / A&GT;&LT; D​​IV&GT;&LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://www.yahoo.com&GT;雅虎&LT ; / A&GT;
08-02 20:02:27.535:D / JSON字符串(795): - &所述; A HREF =htt​​p://us.rd.yahoo.com/404/*http://help.yahoo.com&GT ;帮助&LT; / A&GT;&LT; / DIV&GT;&LT; / DIV&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,DIV ID =BD&GT;&LT; H1&GT;对不起,您请求的页面不存在&LT; / H1方式&gt;
08-02 20:02:27.535:D / JSON字符串(795):其中,P&​​gt;请检查正确的拼写和大小写的URL。如果
08-02 20:02:27.535:D / JSON字符串(795):您无法定位在雅虎目标!尝试访问
08-02 20:02:27.535:D / JSON字符串(795):其中;强&GT;&LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://www.yahoo.com&GT;雅虎!家
08-02 20:02:27.535:D / JSON字符串(795):网页&LT; / A&GT;&LT; / STRONG&GT;或者通过℃的列表中查找;强&GT;&LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more /&GT;雅虎!
08-02 20:02:27.535:D / JSON字符串(795):在线服务&LT; / A&GT;&LT; / STRONG取代。此外,你会发现你在找什么
08-02 20:02:27.535:D / JSON字符串(795):如果您尝试以下搜索&LT; / P&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,表单名称=S1行动=htt​​p://us.rd.yahoo.com/404/*-http://search。 yahoo.com/search\"><fieldset>
08-02 20:02:27.535:D / JSON字符串(795):其中,传说&GT;&LT;标签=S1P&GT;搜索网页&LT; /标签&gt;&LT; /传说&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,INPUT TYPE =文字大小= 30名=PID =S1P称号=请在此输入搜索词&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,INPUT TYPE =提交值=搜索&GT;
08-02 20:02:27.535:D / JSON字符串(795):或其可跨度&GT;&下; A HREF =htt​​p://us.rd.yahoo.com/404/*http://search.yahoo。 COM /搜索/选项p =&GT;高级搜索&LT; / A&GT; &LT;跨度类=九月&GT; |&LT; / SPAN&GT; &LT; A HREF =htt​​p://us.rd.yahoo.com/404/*http://buzz.yahoo.com&GT;最流行的&LT; / A&GT;&LT; / SPAN&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,/字段集&GT;&LT; /表及GT;
08-02 20:02:27.535:D / JSON字符串(795):其中,P类=多&gt;请尽量&LT;强&GT;&LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://help.yahoo.com&GT;雅虎!
08-02 20:02:27.535:D / JSON字符串(795):帮助中亚&LT; / A&GT;&LT; / STRONG&GT;如果您需要更多的援助和LT; / P&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中; / DIV&GT;&LT; D​​IV ID =英尺&GT;&LT; P&GT;版权和放大器;复印件; 2013雅虎
08-02 20:02:27.535:D / JSON字符串(795):保留所有权利。 &LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://privacy.yahoo.com&gt;隐私权
08-02 20:02:27.535:D / JSON字符串(795):政策&lt; / A&GT; - &LT;一
08-02 20:02:27.535:D / JSON字符串(795):HREF =htt​​p://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/ &GT;条款
08-02 20:02:27.535:D / JSON字符串(795):服务与LT的; / A&GT;&LT; / P&GT;&LT; / DIV&GT;
08-02 20:02:27.535:D / JSON字符串(795):其中; / DIV&GT;&LT; /身体GT;&LT; / HTML&GT;


解决方案

尝试DIS希望它会为ü工作

 公共类TestObjectToJson {
      公众诠释K1 = 1;
      公共字符串K2 =测试;      公共静态无效的主要(字串[] args){
          TestObjectToJson的obj =新TestObjectToJson();
          GSON GSON =新GSON();
    JSON字符串= gson.toJson(OBJ);          的System.out.println(JSON);
      }    }

有关引用u能看到这种

  HTTPS://$c$c.google.com/p/google-gson/

I'm just going to get right into it. I get an error in LogCat that says my JSON String can not be converted into a JSONOject and I really don't know how to work my around this error so I was just hoping somebody with better knowledge than me can give me advice on how I can attend to this issue

Logcat:

08-02 00:12:28.485: E/JSON Parser(778): Error parsing data org.json.JSONException: Value <!doctype of type java.lang.String cannot be converted to JSONObject

JSONParser class

 public class JSONParser {

    InputStream is;
    JSONObject jObj;
    String json = "";
    public EditText et;

    public JSONParser () {
    }

    public JSONObject readJSONFeed(String URL) {

        try{
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(URL);
        //request.setURI(website);
        try {
            HttpResponse response = client.execute(request);
        HttpEntity httpEntity = response.getEntity();
        is = httpEntity.getContent();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line;
            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 {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
           Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;

        }finally{}

    }{
    }}

This is the method that is causing this error to appear:

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
       Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

I've printed my JSON String:

08-02 20:02:27.535: D/JSON String(795): <!doctype html public "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
08-02 20:02:27.535: D/JSON String(795): <html><head><title>Yahoo! - 404 Not Found</title><style>
08-02 20:02:27.535: D/JSON String(795): /* nn4 hide */ 
08-02 20:02:27.535: D/JSON String(795): /*/*/
08-02 20:02:27.535: D/JSON String(795): body {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;}
08-02 20:02:27.535: D/JSON String(795): html>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;}
08-02 20:02:27.535: D/JSON String(795): p {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px}
08-02 20:02:27.535: D/JSON String(795): #ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;}
08-02 20:02:27.535: D/JSON String(795): h1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;}
08-02 20:02:27.535: D/JSON String(795): form {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;}
08-02 20:02:27.535: D/JSON String(795): #s1p {width:15em;margin-right:.1em;}
08-02 20:02:27.535: D/JSON String(795): form span {position:absolute;left:70%;top:.8em;}form a {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;} 
08-02 20:02:27.535: D/JSON String(795): form .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;}
08-02 20:02:27.535: D/JSON String(795): /* end nn4 hide */
08-02 20:02:27.535: D/JSON String(795): </style></head>
08-02 20:02:27.535: D/JSON String(795): <body><div id="doc">
08-02 20:02:27.535: D/JSON String(795): <div id="ygma"><a href="http://us.rd.yahoo.com/404/*http://www.yahoo.com"><img
08-02 20:02:27.535: D/JSON String(795): src=http://l.yimg.com/a/i/yahoo.gif
08-02 20:02:27.535: D/JSON String(795): width=147 height=31 border=0 alt="Yahoo!"></a><div><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo!</a>
08-02 20:02:27.535: D/JSON String(795):  - <a href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Help</a></div></div>
08-02 20:02:27.535: D/JSON String(795): <div id="bd"><h1>Sorry, the page you requested was not found.</h1>
08-02 20:02:27.535: D/JSON String(795): <p>Please check the URL for proper spelling and capitalization. If
08-02 20:02:27.535: D/JSON String(795): you're having trouble locating a destination on Yahoo!, try visiting the
08-02 20:02:27.535: D/JSON String(795): <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">Yahoo! home
08-02 20:02:27.535: D/JSON String(795): page</a></strong> or look through a list of <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/">Yahoo!'s
08-02 20:02:27.535: D/JSON String(795): online services</a></strong>. Also, you may find what you're looking for
08-02 20:02:27.535: D/JSON String(795): if you try searching below.</p>
08-02 20:02:27.535: D/JSON String(795): <form name="s1" action="http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search"><fieldset>
08-02 20:02:27.535: D/JSON String(795): <legend><label for="s1p">Search the Web</label></legend>
08-02 20:02:27.535: D/JSON String(795): <input type="text" size=30 name="p" id="s1p" title="enter search terms here">
08-02 20:02:27.535: D/JSON String(795): <input type="submit" value="Search">
08-02 20:02:27.535: D/JSON String(795): <span><a href="http://us.rd.yahoo.com/404/*http://search.yahoo.com/search/options?p=">advanced search</a> <span class=sep>|</span> <a href="http://us.rd.yahoo.com/404/*http://buzz.yahoo.com">most popular</a></span>
08-02 20:02:27.535: D/JSON String(795): </fieldset></form>
08-02 20:02:27.535: D/JSON String(795): <p class="more">Please try <strong><a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">Yahoo!
08-02 20:02:27.535: D/JSON String(795): Help Central</a></strong> if you need more assistance.</p>
08-02 20:02:27.535: D/JSON String(795): </div><div id="ft"><p>Copyright &copy; 2013 Yahoo! Inc.
08-02 20:02:27.535: D/JSON String(795): All rights reserved. <a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://privacy.yahoo.com">Privacy
08-02 20:02:27.535: D/JSON String(795): Policy</a> - <a
08-02 20:02:27.535: D/JSON String(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/">Terms
08-02 20:02:27.535: D/JSON String(795): of Service</a></p></div>
08-02 20:02:27.535: D/JSON String(795): </div></body></html>

解决方案

Try dis hope it will work for u

 public class TestObjectToJson {
      public int k1 = 1;
      public String k2 = "Test";

      public static void main(String[] args) {
          TestObjectToJson obj = new TestObjectToJson();
          Gson gson = new Gson();
    String json = gson.toJson(obj);

          System.out.println(json);
      }

    }

For reference u can see this

https://code.google.com/p/google-gson/

这篇关于将字符串转换成JSONObject的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆