在JAVA中使用org.json创建JSONObject(从字符串)

17

我正在使用org.json,并且有一个JSON字符串:

{"hours":["1","2","3","4","5","6","7","8","9","10","11"]}

当我尝试从该字符串创建一个新的JSONObject时,我会得到以下错误:

Value of type ..String cannot be converted to JSONObject.

我做错了什么?

编辑:假设str具有以下内容:

JSONObject jObject = new JSONObject(str);

编辑2: 这是实际的字符串:

{"hours":["1","2","3","4","5","6","7","8","9","10","11"],"lessons":["\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,","\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,","\u05d7\u05e0\"\u05d2 \u05d1\u05e0\u05d9\u05dd,\u05d7\u05e0\"\u05d2 \u05d1\u05e0\u05d5\u05ea\n\u05d9\u05de\u05e4\u05d5\u05dc\u05e1\u05e7\u05d9 \u05dc,\u05e0\u05d0\u05d5\u05e8 \u05de\u05dc\u05d9","\u05e1\u05e4\u05e8\u05d5\u05ea\n\u05d6\u05d9\u05dc\u05d3\u05de\u05df \u05d0\u05d5\u05e8\u05dc\u05d9","\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea","\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea","\u05ea\u05e0\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,","\u05ea\u05e0\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,","\u05e2\u05e8\u05d1,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\u05d8\u05db\",\u05e8\u05d5\u05e1,\n\u05dc\u05d5\u05d9,\u05e7\u05de\u05d7,\u05d1\u05e1\u05d5,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8,","\u05e2\u05e8\u05d1\u05d9,\u05e7\u05d5\u05dc\u05e0,\u05d1\u05d9\u05d5\u05d8,\u05d8\u05db\"\u05dd\n\u05dc\u05d5\u05d9,\u05d1\u05e1\u05d5\u05df,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8",""]}

编辑3:最后,这是我的代码,希望你能解决那个问题。

StringBuilder str = new StringBuilder();
        TextView textView = new TextView(this);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://ohel-shem.com/~iApplication/appdata.php?pass=12345&request=cp&day=4%20&class=7&layer=12");
        Log.d("MyTag","Before");
        HttpResponse response = client.execute(request);

//       Get the response
        BufferedReader rd = new BufferedReader
          (new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
          str.append(line);
        } 
        Log.d("MyTag","hello");
        String s = str.toString();
                s= s.replaceAll("\n", "\\n");
                JSONObject json = new JSONObject(s); //Occures an error converting string to jsonobject

你是否忘记了包含新的关键字?如果你忘记了新的关键字,编译器会认为你只是将其转换为一个 JSONObject,这是不合法的。 - ajon
这就是它写出来的样子(修改了我的问题) - idish
请将字符串赋值粘贴。 - MikePatel
@MikePatel 字符串并不完全与我所提到的相同,我会在一分钟内上传它。 - idish
@MikePatel 你是认真的吗?你建议我怎么把它们拿出来? - idish
显示剩余3条评论
2个回答

22

欢迎来到美妙的世界,破坏你的解析器。

\n指定一个换行符,我不确定为什么它会破坏解析器,但是在它前面加上\将转义控制字符。

@Test
public void test() throws JSONException {

        String s = 
        "{\"hours\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\"],\"lessons\":[\"\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,\",\"\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,\",\"\u05d7\u05e0\\\"\u05d2 \u05d1\u05e0\u05d9\u05dd,\u05d7\u05e0\\\"\u05d2 \u05d1\u05e0\u05d5\u05ea\n\u05d9\u05de\u05e4\u05d5\u05dc\u05e1\u05e7\u05d9 \u05dc,\u05e0\u05d0\u05d5\u05e8 \u05de\u05dc\u05d9\",\"\u05e1\u05e4\u05e8\u05d5\u05ea\n\u05d6\u05d9\u05dc\u05d3\u05de\u05df \u05d0\u05d5\u05e8\u05dc\u05d9\",\"\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea\",\"\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea\",\"\u05ea\u05e0\\\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,\",\"\u05ea\u05e0\\\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,\",\"\u05e2\u05e8\u05d1,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\u05d8\u05db\\\",\u05e8\u05d5\u05e1,\n\u05dc\u05d5\u05d9,\u05e7\u05de\u05d7,\u05d1\u05e1\u05d5,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8,\",\"\u05e2\u05e8\u05d1\u05d9,\u05e7\u05d5\u05dc\u05e0,\u05d1\u05d9\u05d5\u05d8,\u05d8\u05db\\\"\u05dd\n\u05dc\u05d5\u05d9,\u05d1\u05e1\u05d5\u05df,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8\",\"\"]}";
        s= s.replaceAll("\n", "\\n");
        JSONObject json = new JSONObject(s); 
    }

运行正常。

编辑:看了一下你的代码,你很幸运。有些服务有时会在源中添加一个奇怪的字符,看起来这就是发生了这种情况。你只需要从字符串中删除它即可。

示例:

s = s.substring(s.indexOf("{")); 

哇,它能工作了,但是当我试图在自己的代码中实现它时,它却没有起作用。我将给你我的代码,你无法想象我在这个问题上卡了多长时间,如果你能帮助我,我会非常高兴的。 - idish
这对你真的有效吗?我在替换函数后面添加了那行代码,但对我来说仍然存在同样的问题。 - idish
抱歉,它确实有效,我想我可能只是累了或者什么的。我非常感谢你,我从来不会猜到这是问题所在。你能否请解释一下为什么要将/n转换为//n?还有为什么这些服务会添加随机字符?再次感谢您的帮助。 - idish

2
import org.json.JSONException;
import org.json.JSONObject;

public class JSONTest {

    public static void main(String[] args) throws JSONException {
        String str = "{\"hours\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\"]}";
        JSONObject jsonObject = new JSONObject(str);
        System.out.println(jsonObject);
    }
}

输出结果为:{"hours":["1","2","3","4","5","6","7","8","9","10","11"]}


太好了,它实际上可以工作,但是你能看一下我上面问题中的“EDIT 2”吗?那才是我真正想要转换为jsonobject的字符串。实际上,如果我能将流转换为jsonobject,那就更好了,因为我从服务器获取该字符串。 - idish

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接