Search code examples
jsonstringflutterdartserialization

JsonDecode json with inner JsonString


I have next Json String which I want to decode

[{
    "material_id": 1193,
    "material_code": "AN00000211",
    "material_name": "MARGARITA PIZZA",
    "security_code": "192.168.1.6",
    "security_code_name": null,
    "bar_barcode": "100209",
    "price_value": 50.00000,
    "mat_auto_price": 0.00000,
    "group_code": "PIZZA",
    "cat_id": 2,
    "mat_name_lang1": "MARGARITA  PIZZA",
    "mat_name_lang2": "МАРГАРИТА ПИЦЦА",
    "mat_name_lang3": "MARGARITA  PIZZA",
    "mat_attributes": "",
    "spe_code2": "",
    "spe_code3": "",
    "spe_code4": ""
},
{
    "material_id": 1194,
    "material_code": "AN00000212",
    "material_name": "TOWUKLY PIZZA",
    "security_code": "192.168.1.6",
    "security_code_name": null,
    "bar_barcode": "100210",
    "price_value": 65.00000,
    "mat_auto_price": 0.00000,
    "group_code": "PIZZA",
    "cat_id": 2,
    "mat_name_lang1": "TOWUKLY PIZZA",
    "mat_name_lang2": "КУРИНАЯ ПИЦЦА",
    "mat_name_lang3": "TOWUKLY PIZZA",
    "mat_attributes": "[{"mat_attribute_id":"6904982A-EBA6-4A31-8D38-3B688ADB9D4E","mat_attribute_name":"Test2","mat_attribute_desc":"Test2","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"6D6FD94D-1498-4EB1-A939-E15CFCE4A0F8","mat_attribute_name":"Test1","mat_attribute_desc":"Test1","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"9CD224D1-A87B-40CE-AACE-6B551666C344","mat_attribute_name":"Test3","mat_attribute_desc":"Test3","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"}]",
    "spe_code2": "",
    "spe_code3": "",
    "spe_code4": ""
}]

When I using

List decoded = jsonDecode(result);

I got Format Exception: Unexpected character error on here

"mat_attributes": "[{"mat_attribute_id":"6904982A-EBA6-4A31-8D38-3B688ADB9D4E","mat_attribute_name":"Test2","mat_attribute_desc":"Test2","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"6D6FD94D-1498-4EB1-A939-E15CFCE4A0F8","mat_attribute_name":"Test1","mat_attribute_desc":"Test1","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"},{"mat_attribute_id":"9CD224D1-A87B-40CE-AACE-6B551666C344","mat_attribute_name":"Test3","mat_attribute_desc":"Test3","material_id_guid":"6A895CC4-4093-4693-85F4-C65693FEB45F","material_id":"1194"}]",

How can I correctly decode like that Json String?


Solution

  • Remove unexpected characters

    Use the replace all function on string.

    final fixedResult = result.replaceAll('"[', '[').replaceAll(']"', ']');
    
    List decoded = jsonDecode(fixedResult);