1

am facing quite weired issue, in android am having one hashmap object of this type:

HashMap<String,  ArrayList<String>> cart = null;

this is my cart object where i put my cart items, in android device version 4.4.2 and onwards this object is correctly sent to server in this format :

{"6":["Veg Biryani","599.0","0.0","1"],"5":["Chiken Tanduri","599.0","0.0","2"]} 

But, in older versions before 4.3 it's just sending the above object in this format:

{"26":"[Test, 1465.0, 0.0, 2]","5":"[Chiken Tanduri, 599.0, 0.0, 3]"}

as we can see in second format the hashmap object value is entirely encoded as a string, which should not be the case,

In order to sent this object to server am just casting it to JSONObject and stringify it to sent to server.

String.valueOf(new JSONObject(cart))

Can someone please point me, what am doing wrong in the above code.

Any help would be greatly appreciated.

Thank You!

U.Swap
  • 1,921
  • 4
  • 23
  • 41
  • which JSON library are you using ? – Rohan May 26 '16 at 11:13
  • You are going to need to iterate your map, the older version doesn't seems to manage map so read it as an object, so everything is set into a String. – AxelH May 26 '16 at 11:16

2 Answers2

2

The problem is that the constructor for Map only exist since API19, so prior this, the map is add as an Object.

You need to iterate every element of your map to create you JSON.

You can off course use a external Library but you can simply use a UtilClass to do the conversion. To the conversion, there is this solution

https://stackoverflow.com/a/22912023/4391450

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
0

Try using this instead -

(new JSONObject(cart)).toString();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rohan
  • 1,180
  • 2
  • 15
  • 28