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

前端 未结 12 586
失恋的感觉
失恋的感觉 2021-01-01 13:34
String JSON = \"http://www.json-generator.com/j/cglqaRcMSW?indent=4\";

JSONObject jsonObject = new JSONObject(JSON);
JSONObject getSth = jsonObject.getJSONObject(\"         


        
相关标签:
12条回答
  • 2021-01-01 14:14

    While the json begins with "[" and ends with "]" that means this is the Json Array, use JSONArray instead:

    JSONArray jsonArray = new JSONArray(JSON);
    

    And then you can map it with the List Test Object if you need:

    ObjectMapper mapper = new ObjectMapper();
    List<TestExample> listTest = mapper.readValue(String.valueOf(jsonArray), List.class);
    
    0 讨论(0)
  • 2021-01-01 14:18

    I had same issue. My Json response from the server was having [, and, ]:

    [{"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/ says valid json. you can copy and verify it.

    I have fixed with below code as temporary solution:

    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); 
        .....   
    }
    
    0 讨论(0)
  • 2021-01-01 14:18

    The file that I was using was saved through Powershell in UTF-8 format. I changed it to ANSI and it fixed the problem.

    0 讨论(0)
  • 2021-01-01 14:20

    Actually,,i found a simple answer,, Jst adding the object to String Builder instead of String worked ;)

    StringBuilder jsonString= new StringBuilder.append("http://www.json-.com/j/cglqaRcMSW?=4");    
    JSON json= new JSON(jsonString.toString);
    
    0 讨论(0)
  • 2021-01-01 14:21

    I had the same error and struggled to fix it, then answer above by Nagaraja JB helped me to fix it. In my case:

    Was before: JSONObject response_json = new JSONObject(response_data);

    Changed it to: JSONArray response_json = new JSONArray(response_data);

    This fixed it.

    0 讨论(0)
  • 2021-01-01 14:22

    I had the same issue because of the wrong order of the code statements. Maintain the below order to resolve the issue. All get methods statements first and later httpClient methods.

          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);
    
    0 讨论(0)
提交回复
热议问题