JSONObject文本必须以“{”开头

时间:2011-01-23 11:58:16

标签: java json

我有这个JSONObject:

{
  "gutter_url" : "",
  "sort_order" : "popularity",
  "result" : [
    {
      "afs" : "Y",
      "release_year" : 1979,
      "album_sort" : "Wall, The"
    }
  ]
}

并希望将数组放在“结果”位置, 所以我写了这段代码:

JSONObject allCDs = new JSONObject(objectString);
JSONArray CD_List = allCDs.getJSONArray("result");

但后来我得到了这个例外:

Exception in thread "main" org.json.JSONException: A JSONObject text must begin with '{' at character 1
 at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
 at org.json.JSONObject.<init>(JSONObject.java:179)
 at org.json.JSONObject.<init>(JSONObject.java:402)
 at de.htwberlin.gim.Aufgabe8_5.getCoversFor(Aufgabe8_5.java:55)
 at de.htwberlin.gim.Aufgabe8_5.main(Aufgabe8_5.java:77)

4 个答案:

答案 0 :(得分:15)

您可能正在使用前导空格将STRING传递给JSONObject。尝试修剪

JSONObject allCDs = new JSONObject(objectString.replace(/^\s+/,""));

编辑:我认为这是javascript。尝试使用Java代码修改它

JSONObject allCDs = new JSONObject(objectString.trim());

如果仍然无效,则显示字符串中的第一个字符:

System.out.println((int)objectString.trim().charAt(0));

你应该期待123,花括号。实际上,检查整个内容

System.out.println((int)objectString);  // or
System.out.println((int)objectString.trim());

您也可以尝试在{in string

之前剪切所有内容
JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{')));

答案 1 :(得分:1)

这一行末尾有两个逗号:

"sort_order" : "popularity",,

它应该是一个逗号:

"sort_order" : "popularity",

答案 2 :(得分:1)

找到解决方案!我在用Java读取Bulk APi时遇到此错误 因此,如果您处于相同的情况,则只需添加以下几行即可。

con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");

答案 3 :(得分:0)

示例1 :从文件中读取Json文本。

package com.json.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;

public class GetArrayObjectFromJsonText {
    public static void main(String[] args) {
        String jsonText;
        try {
            jsonText = IOUtils.toString(new FileInputStream(new File("C:\\Users\\udaykiranp\\Downloads\\Json.txt")));
            int i = jsonText.indexOf("{");
            jsonText = jsonText.substring(i);
            JSONObject jsonFile = new JSONObject(jsonText);
            System.out.println("Input JSON data: "+ jsonFile.toString());
            Object result = jsonFile.get("result");
            System.out.println("Result array Data: "+ result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

示例2:

String jsonText = "\"{\"gutter_url\":\"\", \"result\":[{\"album_sort\":\"Wall, The\",\"release_year\":1979,\"afs\":\"Y\"}],\"sort_order\":\"popularity\"}\"";
int i = jsonText.indexOf("{");
jsonText = jsonText.substring(i);
JSONObject jsonFile = new JSONObject(jsonText);
System.out.println("Input JSON data: "+ jsonFile.toString());
Object result = jsonFile.get("result");
System.out.println("Result array Data: "+ result);

首先获取索引位置 { ,然后读取json文本数据

输出:

Input JSON data: {"gutter_url":"","result":[{"album_sort":"Wall, The","release_year":1979,"afs":"Y"}],"sort_order":"popularity"}
Result array Data: [{"album_sort":"Wall, The","release_year":1979,"afs":"Y"}]
相关问题