JSONObject text must begin with '{' in 1 [character 2 line 1] with error '{'

String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";

JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject("get");
Object level = getSth.get("2");

System.out.println(level);

I referred to many solutions for parsing this link , still getting the same error in the question. Can someone give me a simple solution to analyze it.

+12
source share
11 answers

Your problem is that String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";not JSON.
You want to open a connection HTTPfrom http://www.json-generator.com/j/cglqaRcMSW?indent=4 "and parse the JSON response.

String JSON = "http://www.json-generator.com/j/cglqaRcMSW?indent=4";
JSONObject jsonObject = new JSONObject(JSON); // <-- Problem here!

Will not open a connection to the site and receive content.

+10
source

. Json [, ,]:

[{"DATE_HIRED":852344800000,"FRNG_SUB_ACCT":0,"MOVING_EXP":0,"CURRENCY_CODE":"CAD  ","PIN":"          ","EST_REMUN":0,"HM_DIST_CO":1,"SICK_PAY":0,"STAND_AMT":0,"BSI_GROUP":"           ","LAST_DED_SEQ":36}]

http://jsonlint.com/ json. .

:

BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String result ="";
String output = null;
while ((result = br.readLine()) != null) {
    output = result.replace("[", "").replace("]", "");
    JSONObject jsonObject = new JSONObject(output); 
    JSONArray jsonArray = new JSONArray(output); 
    .....   
}
+8

, . :

int i = result.indexOf("{");
result = result.substring(i);
JSONObject json = new JSONObject(result.trim()); 
System.out.println(json.toString(4));  
+2

- . , . httpClient.

      HttpClient httpClient = new HttpClient();
get = new GetMethod(instanceUrl+usersEndPointUri);
get.setRequestHeader("Content-Type", "application/json");
get.setRequestHeader("Accept", "application/json");

httpClient.getParams().setParameter("http.protocol.single-cookie-header", true);
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(get);
+2

json

JSON vb.net : My.Computer.FileSystem.WriteAllText("newComponent.json", json.Trim, False).

, .

Notepad++ , UTF-8-BOM

, , , UTF-8, - .

, - .

+2

, , . . . :

: JSONObject response_json = JSONObject (response_data);

: JSONArray response_json = JSONArray (response_data);

.

+2

- , json. json, JSONArray, JSONObject.

+1

arraylist JSONObject, String

List<String> listStrings= new ArrayList<String>();
String json = new Gson().toJson(listStrings);
return json;

Gson 2.8.5

+1

JSON . JSON . http://json.org/java/

0

json "[" "]", , Json, JSONArray:

JSONArray jsonArray = new JSONArray(JSON);

, :

ObjectMapper mapper = new ObjectMapper();
List<TestExample> listTest = mapper.readValue(String.valueOf(jsonArray), List.class);
0
source

Actually, I found a simple answer: Jst adds an object to String Builder instead of String working;)

StringBuilder jsonString= new StringBuilder.append("http://www.json-.com/j/cglqaRcMSW?=4");    
JSON json= new JSON(jsonString.toString);
-2
source

Source: https://habr.com/ru/post/1529154/


All Articles