A JSONObject text must begin with '{' error

后端 未结 4 2293
独厮守ぢ
独厮守ぢ 2021-02-19 00:54

I have this JSON coming from one of our REST service:

[
    \"{\\\"category_name\\\":[\\\"Industry Components\\\"],\\\"categoryId\\\":[1]}\",
    \"{\\\"category         


        
相关标签:
4条回答
  • 2021-02-19 01:19

    I suppose that you should use not JSONObject, but JSONArray

    0 讨论(0)
  • 2021-02-19 01:19

    JSON Object follows the following Structure:

    {
     "array": [
    {
        color: "red",
        value: "#f00"
    },
    {
        color: "green",
        value: "#0f0"
    }
    ]
    }
    

    JSON Array follows the following Structure:

    [
     { "firstName":"John" , "lastName":"Doe" }, 
     { "firstName":"Anna" , "lastName":"Smith" }, 
     { "firstName":"Peter" , "lastName": "Jones" }
    ]
    
    0 讨论(0)
  • 2021-02-19 01:38

    instead

    JSONObject jsonObject = new JSONObject(jsonStr);
    

    use

    JSONArray jsonArray = new JSONArray(jsonStr);
    

    and may be read about Gson is a nice library for parsing and work with json

    0 讨论(0)
  • 2021-02-19 01:41

    If you get JSONObject text must begin with '{' exception. Then first off all check what did u pass to the JSONObject constructor.

    You should pass the right json.txt file.So make sure what are u passing to jsonobject.

    String request = FileUtils.readFileToString(new File("/home/achaure/Downloads/Amol/KountRestTest/Documents/request.txt"));
    
    JSONObject jsonObject = new JSONObject(request);
    
    0 讨论(0)
提交回复
热议问题