14

I have 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)
1
  • what is objectString in your code? Commented Nov 13, 2013 at 6:20

4 Answers 4

16

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('{')));
Sign up to request clarification or add additional context in comments.

13 Comments

you are right, but actually this comma-mistake was just because I shortened the JSONObject for this Question at stackoverflow. The original is totally correct and checked.
The JSONObject(objectString.replace(/^\s+/,"")); does not work :(( .replace is only for chars
What is the type and content of the variable objectString?
the type is a String, which contains information from this JSONObject:
@wong - Pleasure to help. I see that you are new to StackOverflow. Welcome! When you please, you can upvote the answer and accept it (click on the tick next to the answer). Thanks
|
1

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

Comments

0

You have two commas at the end of this line:

"sort_order" : "popularity",,

It should probably be one comma:

"sort_order" : "popularity",

1 Comment

you are right, but actually this comma-mistake was just because I shortened the JSONObject for this Question at stackoverflow. The original is totally correct and checked.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.