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"
    }
    ]
    }}
1
  • did you try this? cartitem.replace("{{","{"); cartitem.replace("}}","}"); Commented Apr 24, 2019 at 11:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

The JSON is indeed invalid, but was supposedly created by jsonEncode. Is this a serious bug in jsonEncode or some other error. We don't know yet.
@RichardHeap exactly may be the serious bug in jsonEncode i tried this by adding double quotes in the cart key and it works.
@RichardHeap see my answer how I get the expected result. I don't know the way I used is correct or not.
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"
        }
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.