GSON throwing “Expected BEGIN_OBJECT but was BEGIN_ARRAY”?

后端 未结 10 660
暖寄归人
暖寄归人 2020-11-22 01:31

I\'m trying to parse a JSON string like this one

[
   {
      \"updated_at\":\"2012-03-02 21:06:01\",
      \"fetched_at\":\"2012-03-02 21:28:37.728840\",
           


        
10条回答
  •  故里飘歌
    2020-11-22 02:02

    Alternative could be

    to make your response look like

    myCustom_JSONResponse

    {"master":[
       {
          "updated_at":"2012-03-02 21:06:01",
          "fetched_at":"2012-03-02 21:28:37.728840",
          "description":null,
          "language":null,
          "title":"JOHN",
          "url":"http://rus.JOHN.JOHN/rss.php",
          "icon_url":null,
          "logo_url":null,
          "id":"4f4791da203d0c2d76000035",
          "modified":"2012-03-02 23:28:58.840076"
       },
       {
          "updated_at":"2012-03-02 14:07:44",
          "fetched_at":"2012-03-02 21:28:37.033108",
          "description":null,
          "language":null,
          "title":"PETER",
          "url":"http://PETER.PETER.lv/rss.php",
          "icon_url":null,
          "logo_url":null,
          "id":"4f476f61203d0c2d89000253",
          "modified":"2012-03-02 23:28:57.928001"
       }
    ]
    }
    

    instead of

    server_JSONResponse

    [
       {
          "updated_at":"2012-03-02 21:06:01",
          "fetched_at":"2012-03-02 21:28:37.728840",
          "description":null,
          "language":null,
          "title":"JOHN",
          "url":"http://rus.JOHN.JOHN/rss.php",
          "icon_url":null,
          "logo_url":null,
          "id":"4f4791da203d0c2d76000035",
          "modified":"2012-03-02 23:28:58.840076"
       },
       {
          "updated_at":"2012-03-02 14:07:44",
          "fetched_at":"2012-03-02 21:28:37.033108",
          "description":null,
          "language":null,
          "title":"PETER",
          "url":"http://PETER.PETER.lv/rss.php",
          "icon_url":null,
          "logo_url":null,
          "id":"4f476f61203d0c2d89000253",
          "modified":"2012-03-02 23:28:57.928001"
       }
    ]
    

    CODE

      String server_JSONResponse =.... // the string in which you are getting your JSON Response after hitting URL
    String myCustom_JSONResponse="";// in which we will keep our response after adding object element to it
         MyClass apiResponse = new MyClass();
    
         myCustom_JSONResponse="{\"master\":"+server_JSONResponse+"}";
    
    
    
        apiResponse = gson.fromJson(myCustom_JSONResponse, MyClass .class);
    

    After this it will be just any other GSON Parsing

提交回复
热议问题