title: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $
body: <p>I'm developing an eCommerce app so i need to create a cartManager with sharedpreference in android studio. I've created json string of Map&lt;Item,itemCount&gt; then put the string into sharedpreferences Everything working well but in the getCartItem() I'm getting this error -</p>
  <p><strong>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $.</strong></p>
  <p>I've written all the code related below. I'm new to android studio Please help me.</p>
  <p><strong>This is Layout (Increment decrement cartmanager value)</strong></p>
  <pre><code> &lt;LinearLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:gravity=&quot;center&quot;
              android:orientation=&quot;horizontal&quot;&gt;
  
              &lt;androidx.cardview.widget.CardView
                  android:id=&quot;@+id/cardViewDec&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_margin=&quot;16dp&quot;
                  app:cardCornerRadius=&quot;8dp&quot;&gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginStart=&quot;12dp&quot;
                      android:layout_marginTop=&quot;8dp&quot;
                      android:layout_marginEnd=&quot;12dp&quot;
                      android:layout_marginBottom=&quot;8dp&quot;
                      android:gravity=&quot;center&quot;
                      android:text=&quot;-&quot;
                      android:textColor=&quot;@color/black&quot; /&gt;
              &lt;/androidx.cardview.widget.CardView&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/textViewItemCount&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_gravity=&quot;center&quot;
                  android:gravity=&quot;center&quot;
                  android:text=&quot;0&quot;
                  android:textColor=&quot;@color/black&quot; /&gt;
  
              &lt;androidx.cardview.widget.CardView
                  android:id=&quot;@+id/cardViewInc&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_margin=&quot;16dp&quot;
                  app:cardCornerRadius=&quot;8dp&quot;&gt;
  
                  &lt;TextView
                      android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:layout_gravity=&quot;center&quot;
                      android:layout_marginStart=&quot;12dp&quot;
                      android:layout_marginTop=&quot;8dp&quot;
                      android:layout_marginEnd=&quot;12dp&quot;
                      android:layout_marginBottom=&quot;8dp&quot;
                      android:gravity=&quot;center&quot;
                      android:text=&quot;+&quot;
                      android:textColor=&quot;@color/black&quot; /&gt;
              &lt;/androidx.cardview.widget.CardView&gt;
          &lt;/LinearLayout&gt;
  </code></pre>
  <p><strong>This is CartManager</strong></p>
  <pre><code>public class CartManager {
      private static final String CART_ITEMS = &quot;cartItems&quot;;
      private static SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(App.getApp());
  
      public static void setCartItems(Map&lt;Item, Integer&gt; mapCartItem) {
          SharedPreferences.Editor editor = preferences.edit();
          Gson gson = new Gson();
          String jsonCartItems = gson.toJson(mapCartItem);
          editor.putString(CART_ITEMS, jsonCartItems);
          editor.apply();
      }
  
      public static Map&lt;Item, Integer&gt; getCartItems() {
          String json = preferences.getString(CART_ITEMS, null);
          Gson gson = new Gson();
          Type collectionType = new TypeToken&lt;Map&lt;Item, Integer&gt;&gt;() {
          }.getType();
          return gson.fromJson(json, collectionType);
      }
  </code></pre>
  <p><strong>This is activity class</strong></p>
  <pre><code>binding.cardViewInc.setOnClickListener(v -&gt; {
              int itemCount = Integer.parseInt(binding.textViewItemCount.getText().toString());
              vibrator.vibrate(200);
          binding.textViewItemCount.setText(String.valueOf(++itemCount));
  
          if (CartManager.getCartItems() != null) {
              Map&lt;Item, Integer&gt; cart = CartManager.getCartItems();
              Log.d(TAG, &quot;onCreate: &quot; + cart);
              cart.put(item, itemCount);
              CartManager.setCartItems(cart);
          } else {
              Map&lt;Item, Integer&gt; cart = new HashMap&lt;&gt;();
              cart.put(item, itemCount);
              CartManager.setCartItems(cart);
          }
      });
  
      binding.cardViewDec.setOnClickListener(v -&gt; {
          int itemCount = Integer.parseInt(binding.textViewItemCount.getText().toString());
          if (itemCount == 0)
              return;
          vibrator.vibrate(200);
          binding.textViewItemCount.setText(String.valueOf(--itemCount));
  
          Map&lt;Item, Integer&gt; cart = CartManager.getCartItems();
          Log.d(TAG, &quot;onCreate: &quot; + cart);
          cart.put(item, itemCount);
          CartManager.setCartItems(cart);
      });
  </code></pre>
  <p><strong>This is error I'm getting right now</strong></p>
  <pre><code>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $.
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
          at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
          at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
          at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
          at com.google.gson.Gson.fromJson(Gson.java:927)
          at com.google.gson.Gson.fromJson(Gson.java:892)
          at com.google.gson.Gson.fromJson(Gson.java:841)
          at com.account.CartManager.getCartItems(CartManager.java:31)
          at com.activities.ProductActivity.lambda$onCreate$1$ProductActivity(ProductActivity.java:61)
          at com.activities.-$$Lambda$ProductActivity$eIp-9al-fb-L4Pi9XbJi9DUaRDU.onClick(Unknown Source:6)
          at android.view.View.performClick(View.java:7184)
          at android.view.View.performClickInternal(View.java:7161)
          at android.view.View.access$3500(View.java:818)
          at android.view.View$PerformClick.run(View.java:27677)
          at android.os.Handler.handleCallback(Handler.java:883)
          at android.os.Handler.dispatchMessage(Handler.java:100)
          at android.os.Looper.loop(Looper.java:224)
          at android.app.ActivityThread.main(ActivityThread.java:7562)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
       Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $.
          at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
          at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
          at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) 
          at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186) 
          at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145) 
          at com.google.gson.Gson.fromJson(Gson.java:927) 
          at com.google.gson.Gson.fromJson(Gson.java:892) 
          at com.google.gson.Gson.fromJson(Gson.java:841) 
          at com.account.CartManager.getCartItems(CartManager.java:31) 
          at com.activities.ProductActivity.lambda$onCreate$1$ProductActivity(ProductActivity.java:61) 
          at com.activities.-$$Lambda$ProductActivity$eIp-9al-fb-L4Pi9XbJi9DUaRDU.onClick(Unknown Source:6) 
          at android.view.View.performClick(View.java:7184) 
          at android.view.View.performClickInternal(View.java:7161) 
          at android.view.View.access$3500(View.java:818) 
          at android.view.View$PerformClick.run(View.java:27677) 
          at android.os.Handler.handleCallback(Handler.java:883) 
          at android.os.Handler.dispatchMessage(Handler.java:100) 
          at android.os.Looper.loop(Looper.java:224) 
          at android.app.ActivityThread.main(ActivityThread.java:7562) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
  2021-06-19 08:51:50.623 30179-30179/com I/Process: Sending signal. PID: 30179 SIG: 9
  </code></pre>
  <p><strong>This is jsonString i'm getting</strong></p>
  <pre><code>{&quot;Item{_id\u003d\u002760c8bfc64a5f0e3f042cb674\u0027, name\u003d\u0027fff\u0027, price\u003d888, size\u003d6, discount\u003d888, color\u003d\u0027Black\u0027, deliveryFee\u003d88, imageUrl\u003d\u0027https://firebasestorage.googleapis.com/v0/b/shoes-footwear.appspot.com/o/items%2F1623769024945?alt\u003dmedia\u0026token\u003d6094d72b-2f98-483a-a9e5-0d39d13ce066\u0027, gender\u003d0, createdAt\u003d0}&quot;:1}
  </code></pre>