0

When i am getting multiple data in mindTouchPage then my code is working. But when i am receiving a single record as response then it throwing the exception

System.JSONException: Expected List but found { at [line:1, column:53]

My Wrapper Class

public class MindTouchSearchWrapper {
        public Integer count { get; set; }
        //public Integer querycount { get; set; }
        //public Integer ranking { get; set; }
       // public MindTouchPage mindTouchPage{get;set;}
        public List<MindTouchPage> mindTouchPage { get; set; }


  class MindTouchPage
    {
        public string id { get; set; }
        public string guid { get; set; }
        public string draftstate { get; set; }
        public string href { get; set; }
        public string deleted { get; set; }
    //  public string unpublish { get; set; }
        public string revision { get; set; }
        public string score { get; set; }
        public string article { get; set; }
        public string datecreated { get; set; }
        public string dateedited { get; set; }
        public string datemodified { get; set; }
        public string description { get; set; }
        public string language { get; set; }
        public string languageeffective { get; set; }
        public string namespace { get; set; }
        public string summary { get; set; }
        public string timeuuid { get; set; }
        public string title { get; set; }
        public string uriui { get; set; }
    }

public static MindTouchSearchWrapper parse(String responseData){
            System.debug('Data Befor deserialization and after processing is ' +responseData);
            System.debug('Deserialized Data is ' +(MindTouchSearchWrapper) System.JSON.deserialize(responseData, MindTouchSearchWrapper.class) );
            return (MindTouchSearchWrapper) System.JSON.deserialize(responseData, MindTouchSearchWrapper.class);
}

}

JSON Response for Multiple Data

 {  
       "count":"30",
       "querycount":"30",
       "ranking":"adaptive",
       "mindTouchPage":[  
          {  
             "id":"294",
             "guid":"b8ec34b2ec734aa4814df5b1e8dd2adf",
             "draftstate":"inactive",
             "deleted":"false",
             "unpublish":"true",
             "revision":"2",
             "score":"1",
             "timeuuid":"01c72c00-2f53-11e6-806c-a7241b5c7980",
             "title":"Structure your site for product versioning",
             "uriui":"https://viptela-prod.mindtouch.us/The_MindTouch_Workbook_-_Demo_Change/Chapter_II_-_How_do_I_create_stuff%3F/010_Create_new_content/012_Handling_product_versioning_in_your_structure",

          },
             "id":"333",
             "guid":"01955249d38a4c8681940f8e84b55b90",
             "draftstate":"inactive",
              "deleted":"false",
             "unpublish":"true",
             "revision":"2",
             "score":"0.9655172",
             "datecreated":"Mon, 20 Jun 2016 06:59:54 GMT",
             "dateedited":"Mon, 20 Jun 2016 07:00:25 GMT",
             "datemodified":"Mon, 20 Jun 2016 12:36:27 GMT",
             "description":"17 words added",
             "timeuuid":"9b28d780-36e3-11e6-8017-3ce2f83fe0c8",
             "title":"Product2",
             "uriui":"https://viptela-prod.mindtouch.us/Advisory1/First_Topic/Product1/Product2",

          },

        ]
    }

JSON Response For Single Data

{  
   "count":"1",
   "@querycount":"1",
   "@ranking":"adaptive",

   "mindTouchPage":{  
      "id":"333",
      "guid":"01955249d38a4c8681940f8e84b55b90",
      "draftstate":"inactive",
      "deleted":"false",
      "unpublish":"true",
      "revision":"2",
      "score":"1",
      "aliases":{  },
      "comments":{  },
      "contents":{  },
      "contentsalt":{  },
      "namespace":"main",
      "timeuuid":"9b28d780-36e3-11e6-8017-b0ae3a3ebec9",
      "title":"Product2",
      "uriui":""

   }
}

How to write the wrapper class which can handle both single and multiple data

0

The problem here is the API. It's returning inconsistent schema. I would strongly suggest to update the API to always return array of records.

If that's not an option, for some reason, the approach I would suggest having separate class Result, with common schema and parse Json based on that class first. After that, depending on count field, parse either list or singe record.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.