开发者

JSONObject text must begin with '{'

I h开发者_开发百科ave this JSONObject:

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

and want to get the Array at the position "result", so I wrote this code:

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

But then I get this Exception:

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)


You may be passing the STRING to JSONObject with leading spaces. Try trimming

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

EDIT: I thought this was javascript. Try trimming it using Java code instead

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

If that still doesn't work, then show what the first character from the string is:

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

You should be expecting 123, the curly braces. In fact, check the entire content

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

You could also try cutting everything before the { in the string

JSONObject allCDs = new JSONObject(objectString.substring(objectString.indexOf('{')));


Found solution! I got this error when i was reading Bulk APi in Java so if you are in the same situation then just add following lines.

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


You have two commas at the end of this line:

"sort_order" : "popularity",,

It should probably be one comma:

"sort_order" : "popularity",


Example1: Read the Json text from file.

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();
        }
    }
}

Example2:

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);

First get the Index position of { then read the json text data.

Output:

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"}]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜