Json使用内部 JsonString 解码 json

问题描述 投票:0回答:1

我有下一个要解码的 Json 字符串

[{
    "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": ""
}]

当我使用时

List decoded = jsonDecode(result);

我遇到了格式异常:意外字符错误

"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"}]",

如何正确解码 Json 字符串?

json string flutter dart serialization
1个回答
0
投票

"mat_attributes"

字段中嵌套了JSON,导致
jsonDecode
函数抛出异常。 
"mat_attributes"
 字段包含 JSON 数组的字符串表示形式。

要正确解码 JSON 字符串,您需要在解码主 JSON 字符串后单独解码

"mat_attributes"

 字段中的嵌套 JSON。

import 'dart:convert'; void main() { String jsonString = '[{"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":""}]'; List<dynamic> decodedList = jsonDecode(jsonString); // Decode the nested JSON within the "mat_attributes" field decodedList.forEach((element) { if (element['mat_attributes'] != '') { element['mat_attributes'] = jsonDecode(element['mat_attributes']); } }); print(decodedList); }
在此代码中,首先将主 JSON 字符串解码为动态对象列表。然后,迭代列表中的每个元素并解码 

"mat_attributes"

 字段中的嵌套 JSON(如果它不为空)。

© www.soinside.com 2019 - 2024. All rights reserved.