0

I know, this question has been asked multiple times and I've tried many SO answers this , this , this but nothing helped me.

I'm having JSON string here

"[
{\"heading\":\"Bike Services\",
    \"content\":[
        {\"title\":\"General Service\",\"icon\":\"bike_service_img\"},
        {\"title\":\"Premium Bike Service\",\"icon\":\"bike_service_img\"},
        {\"title\":\"Repair Job\",\"icon\":\"bike_service_img\"},
        {\"title\":\"Bike Ceramic Coating\",\"icon\":\"bike_service_img\"}
    ]
}, 
{\"heading\":\"Bike Service Centre Near You\",
    \"content\":[
        {\"title\":\"Bike Shop Name one\",\"icon\":\"bike_service_centre_img\"},
        {\"title\":\"Bike Shop Name two\",\"icon\":\"bike_service_centre_img\"},
        {\"title\":\"Bike Shop Name three\",\"icon\":\"bike_service_centre_img\"},
        {\"title\":\"Bike Shop Name four\",\"icon\":\"bike_service_centre_img\"},
        {\"title\":\"Bike Shop Name five\",\"icon\":\"bike_service_centre_img\"},
        {\"title\":\"Bike Shop Name six\",\"icon\":\"bike_service_centre_img\"}
    ]
}, 
{\"heading\":\"Car Services\",
    \"content\":[
        {\"title\":\"Express Service\",\"icon\":\"car_service_img\"},
        {\"title\":\"Dent/Scratch Removal\",\"icon\":\"car_service_img\"},
        {\"title\":\"Interior Detailing\",\"icon\":\"car_service_img\"},
        {\"title\":\"Oil Change Package\",\"icon\":\"car_service_img\"},
        {\"title\":\"Complete Car Spa\",\"icon\":\"car_service_img\"},
        {\"title\":\"AC Service\",\"icon\":\"car_service_img\"}
    ]
}, 
{\"heading\":\"Car Service Centre Near You\",
    \"content\":[
        {\"title\":\"Car Shop Name one\",\"icon\":\"car_service_centre_img\"},
        {\"title\":\"Car Shop Name two\",\"icon\":\"car_service_centre_img\"},
        {\"title\":\"Car Shop Name three\",\"icon\":\"car_service_centre_img\"},
        {\"title\":\"Car Shop Name four\",\"icon\":\"car_service_centre_img\"},
        {\"title\":\"Car Shop Name five\",\"icon\":\"car_service_centre_img\"},
        {\"title\":\"Car Shop Name six\",\"icon\":\"car_service_centre_img\"}
    ]
}
]"

And the POJO classes ContainerItems

public class ContainerItems {
    String heading;
    List<ContainerContents> content;

    public String getHeading() {
        return heading;
    }

    public void setHeading(String heading) {
        this.heading = heading;
    }

    public List<ContainerContents> getContent() {
        return content;
    }

    public void setContent(ArrayList<ContainerContents> content) {
        this.content = content;
    }
}

and the ContainerContents.java

public class ContainerContents {
    private String title;
    private String icon;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }
}

Parsing through Gson library as follows

Gson gson = new Gson();
List<ContainerItems> containerItemList = gson.fromJson(mainContainerItems, new TypeToken<List<ContainerItems>>() {}.getType());

But getting error as

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $
                      at com.google.gson.Gson.fromJson(Gson.java:939)
                      at com.google.gson.Gson.fromJson(Gson.java:892)
                      at com.google.gson.Gson.fromJson(Gson.java:841)
                      at com.sam.service.fragments.HomeFragment.loadContainerItems(HomeFragment.java:149)
                      at com.sam.service.fragments.HomeFragment.onCreateView(HomeFragment.java:133)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
                      at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
                      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
                      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
                      at android.app.Activity.performStart(Activity.java:7157)
                      at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2938)
                      at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
                      at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
                      at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
                      at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
                      at android.os.Handler.dispatchMessage(Handler.java:106)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6680)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
                   Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $
                      at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
                      at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
                      at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)

I'm not able understand what's wrong here. Please help me here.

Shambhu
  • 181
  • 6
  • 16

4 Answers4

0

Your JSON started and ended with " so it is a String not an array. First remove "" from JSON then try to parse it as array.

Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
  • Thanks but if I remove the " then it won't be a String. then how can I parsed into `List containerItemList = gson.fromJson(mainContainerItems, new TypeToken>() {}.getType());` – Shambhu Mar 15 '19 at 22:09
  • Okay, but `gson.fromJson()` method accepts `String` variable. If I remove " , then the variable I'm using won't be a String anymore. Can you give me more idea how I can use this solution. – Shambhu Mar 16 '19 at 07:08
  • Oh don't worry. If you remove "" remaining part is still a string. And you can parse it as List. – Anisuzzaman Babla Mar 16 '19 at 07:48
0

Do you verify the json format? I have reformated as below then it works.

     [
      {
        "heading": "Bike Services",
        "content": [
          {
            "title": "General Service",
            "icon": "bike_service_img"
          },
          {
            "title": "Premium Bike Service",
            "icon": "bike_service_img"
          },
          {
            "title": "Repair Job",
            "icon": "bike_service_img"
          },
          {
            "title": "Bike Ceramic Coating",
            "icon": "bike_service_img"
          }
        ]
      },
      {
        "heading": "Bike Service Centre Near You",
        "content": [
          {
            "title": "Bike Shop Name one",
            "icon": "bike_service_centre_img"
          },
          {
            "title": "Bike Shop Name two",
            "icon": "bike_service_centre_img"
          },
          {
            "title": "Bike Shop Name three",
            "icon": "bike_service_centre_img"
          },
          {
            "title": "Bike Shop Name four",
            "icon": "bike_service_centre_img"
          },
          {
            "title": "Bike Shop Name five",
            "icon": "bike_service_centre_img"
          },
          {
            "title": "Bike Shop Name six",
            "icon": "bike_service_centre_img"
          }
        ]
      },
      {
        "heading": "Car Services",
        "content": [
          {
            "title": "Express Service",
            "icon": "car_service_img"
          },
          {
            "title": "Dent/Scratch Removal",
            "icon": "car_service_img"
          },
          {
            "title": "Interior Detailing",
            "icon": "car_service_img"
          },
          {
            "title": "Oil Change Package",
            "icon": "car_service_img"
          },
          {
            "title": "Complete Car Spa",
            "icon": "car_service_img"
          },
          {
            "title": "AC Service",
            "icon": "car_service_img"
          }
        ]
      },
      {
        "heading": "Car Service Centre Near You",
        "content": [
          {
            "title": "Car Shop Name one",
            "icon": "car_service_centre_img"
          },
          {
            "title": "Car Shop Name two",
            "icon": "car_service_centre_img"
          },
          {
            "title": "Car Shop Name three",
            "icon": "car_service_centre_img"
          },
          {
            "title": "Car Shop Name four",
            "icon": "car_service_centre_img"
          },
          {
            "title": "Car Shop Name five",
            "icon": "car_service_centre_img"
          },
          {
            "title": "Car Shop Name six",
            "icon": "car_service_centre_img"
          }
        ]
      }
    ]
Liem Vo
  • 5,149
  • 2
  • 18
  • 16
  • Thanks but how the `JSONObject` will be parsed in this line `List containerItemList = gson.fromJson(mainContainerItems, new TypeToken>() {}.getType());` It throws an error. – Shambhu Mar 15 '19 at 22:08
  • I have do the test with your code. Everything is worked properly – Liem Vo Mar 15 '19 at 22:17
  • Basically, it's a `JSONArray` right? How can I pass JSONArray into the `gson.fromJson()` – Shambhu Mar 16 '19 at 03:01
  • @Shambhu It is handled by GSON library. You have defined the right format of TypeToken and object so the Gson will handle and parser data – Liem Vo Mar 16 '19 at 03:36
  • I was passing the JSON data in string format. Now in which format, I need to pass. Because the JSONArray or JSONObject isn't available in the method `fromJson()` – Shambhu Mar 16 '19 at 04:51
0

Your json is not an JSON object but an JSON Array. So the POJO that you are trying to deserialize the JSON should have a list of List<ContainerItems> not the classContainerItems itself. That means you have to create another class with List<ContainerItems> in which case you have to give this variable a name (lets say myvariable) so you might have to convert that input json of yours into {"<myvariabke>": <your input which is an array>}

Hope this helps !!

vizsatiz
  • 1,933
  • 1
  • 17
  • 36
0

I was having similar problem while working with material library. What worked for me was: Refactor-> clean project

Arsam
  • 368
  • 4
  • 4