无法反序列化JSON文件

时间:2019-04-18 11:34:25

标签: c# json.net

我有一个要使用newtonsoft json库进行序列化的json文件。

请帮助。

Json文件

[{
        "2406_1": [{
                "GSTIN_Status": "Active",
                "GSTIN": "18AABCF2533P1ZE",
                "Unique_ID": "5",
                "State": "Assam",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "03AABCF2533P1ZP",
                "Unique_ID": "5",
                "State": "Punjab",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "06AABCF2533P1ZJ",
                "Unique_ID": "5",
                "State": "Haryana",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "23AABCF2533P1ZN",
                "Unique_ID": "5",
                "State": "Madhya Pradesh",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "27AABCF2533P1ZF",
                "Unique_ID": "5",
                "State": "Maharashtra",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "07AABCF2533P1ZH",
                "Unique_ID": "5",
                "State": "Delhi",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            },
            {
                "GSTIN_Status": "Active",
                "GSTIN": "08AABCF2533P1ZF",
                "Unique_ID": "5",
                "State": "Rajasthan",
                "Input_PAN": "AABCF2533P",
                "Processing_Status": "Success"
            }
        ]
    },
    {
        "2406_2": [{
            "GSTIN_Status": "Active",
            "GSTIN": "27AABCF2544E1ZZ",
            "Unique_ID": "11",
            "State": "Maharashtra",
            "Input_PAN": "AABCF2544E",
            "Processing_Status": "Success"
        }]
    },
    {
        "2406_83998": [{
            "GSTIN_Status": "Active",
            "GSTIN": "29AABCF2416M1ZJ",
            "Unique_ID": "83961",
            "State": "Karnataka",
            "Input_PAN": "AABCF2416M",
            "Processing_Status": "Success"
        }]
    }
]

代码

    public class RootObject
{
    public List<OutputList> OutputList { get; set; }

    public RootObject()
    { }

}
 public class OutputList
{
    public string GSTIN_Status { get; set; }
    public string GSTIN { get; set; }
    public string Unique_ID { get; set; }
    public string State { get; set; }
    public string Input_PAN { get; set; }
    public string Processing_Status { get; set; }


public OutputList()
{

}
}


OutputList Obj = new OutputList();

                string FileName = IDfileUpload.PostedFile.FileName;
                string path =Server.MapPath("~/InputFiles/"+FileName);
                string json = "";
                JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer();
                using (StreamReader r = new StreamReader(path))
                {
                    json = r.ReadToEnd().Trim();
                }

                List<RootObject>instance = JsonConvert.DeserializeObject<List<RootObject>>(json);
                string GSTIN = instance[0].OutputList[0].GSTIN;

1 个答案:

答案 0 :(得分:1)

这些元素为空,因为您的JSON与您定义的RootObject类结构不匹配。

这是JSON的骨架:

[ // An array
    { // An object
        "2406_1": // A name
        [         // A value (another array)
            { // Another object
                "GSTIN_Status": "Active", // More name-value pairs
                ...
            },
            ...
        ]
    },
    {
        "2406_2": [
            {
                "GSTIN_Status": "Active",
                ...
            }
        ]
    },
    {
        "2406_83998": [
            {
                "GSTIN_Status": "Active",
                ...
            }
        ]
    }
]

简而言之,您的JSON是对象数组,其中 object 名称-值对组成>。在这种情况下,看起来这些对象中的每个对象仅由一个名称/值对组成,其中名称似乎是某种形式的唯一键,而值是OutputList的数组。

您如何用C#表示将基于字符串的唯一键映射到OutputList数组的内容?使用Dictionary<string, OutputList[]>。然后,整个JSON就是这些字典的数组:

var instance = JsonConvert.DeserializeObject<Dictionary<string, OutputList[]>[]>(json);

这给了我一个正确反序列化的对象,没有任何空值。如果不是您期望或想要的结构,则需要更改JSON,更改类结构或将结果字典数组转换为例如只是一个具有多个键值对的字典:

var transformed = instance.SelectMany(o => o)
                          .ToDictionary(kv => kv.Key, kv => kv.Value);