1

This is my cart list and I'm converting into JSON object to send at the server

   Map<String,dynamic> str={'cart':cartList};
      cartitem = jsonEncode(str);

doing something like this but it is adding extra JSON object and this is invalid JSON form. then how can I remove extra JSON object.

 {
    {"cart":[
    {
    "cartid":2,
    "pid":"342702",
    "merchantId":"MER-07156",
    "hashkey":"7087fea71afc963d6dc3fa04944008ec",
    "productname":"Scheduling Product - Edit Please",
    "product_image":"Scheduling Product - Edit Please",
    "shipping_price":"0.00",
    "state_tax_rate":"0.0",
    "taxamt":"0.00",
    "discountamt":"0.0",
    "price":"10.00",
    "pricewithattr":"17.00",
    "quantity":"18",
    "totalamount":"306.00",
    "taxvalue":"0.0",
    "attribute_array":"[{\"attributeid\":\"20796\",\"attributename\":\"Black\",\"groupname\":\"Color\",\"groupid\":\"3012\"},{\"attributeid\":\"20798\",\"attributename\":\"Silk\",\"groupname\":\"Cloth\",\"groupid\":\"3013\"},{\"attributeid\":\"20800\",\"attributename\":\"small\",\"groupname\":\"Size\",\"groupid\":\"3014\"}]",
    "is_free":"0",
    "is_payable_later":"0",
    "isattrpresent":"1"
    }
    ]
    }}
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105

3 Answers3

1

Strange, because this code:

  Map<String, dynamic> str = {
    'cart': [1, 2, 3]
  };
  String cartitem = jsonEncode(str);
  print(cartitem);

which does basically the same thing, produces valid json:

{"cart":[1,2,3]}

Try debugging by just json encoding one of the cart members, replacing the cart members by something simple (like an integer, above) until you find the issue.

Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • I tried to debug and found that `jsonEncode` was adding an extra object so I converted `cartiem` into `jsonEncode` prior to adding into the map. – Farhana Naaz Ansari Apr 25 '19 at 05:12
0

Your JSON is not right structure. Did you try to parse it any online converter?

You should remove { character before "cart" and remove } character the end of json.

Enis Erkaya
  • 71
  • 2
  • 6
0

I tried to debug and found that jsonEncode was adding an extra object so I converted cartiem into jsonEncode prior to adding into the map.

  var cartitems2=cartList;
  Map<String,dynamic> str={'"cart"':json.encode(cartitems2)};
  cartitem = str.toString();
  debugPrint('CART :-----${cartitem}');

Expected Result

{
    "cart": [
        {
            "cartid": 22,
            "pid": "342702",
            "merchantId": "MER-07156",
            "hashkey": "7087fea71afc963d6dc3fa04944008ec",
            "productname": "Scheduling Product - Edit Please",
            "product_image": "Scheduling Product - Edit Please",
            "shipping_price": "0.00",
            "state_tax_rate": "0.0",
            "taxamt": "0.00",
            "discountamt": "0.0",
            "price": "10.00",
            "pricewithattr": "26.00",
            "quantity": "10",
            "totalamount": "260.00",
            "taxvalue": "0.0",
            "attribute_array": [
                {
                    "attributeid": "20794",
                    "attributename": "Red",
                    "groupname": "Color",
                    "groupid": "3012"
                },
                {
                    "attributeid": "20799",
                    "attributename": "Cotton",
                    "groupname": "Cloth",
                    "groupid": "3013"
                },
                {
                    "attributeid": "20800",
                    "attributename": "small",
                    "groupname": "Size",
                    "groupid": "3014"
                }
            ],
            "is_free": "0",
            "is_payable_later": "0",
            "isattrpresent": "1"
        }
    ]
}
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105