简体   繁体   English

无法反序列化JSON文件

[英]Not able to deserialise JSON file

I have one json file that I want to de serialise with newtonsoft json library. 我有一个要使用newtonsoft json库取消序列化的json文件。

Please help. 请帮忙。

Json file 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"
        }]
    }
]

Code

    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;

The elements are null because your JSON doesn't match the RootObject class structure you've defined. 元素为null,因为您的JSON与您定义的RootObject类结构不匹配。

Here's a skeleton of your JSON: 这是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",
                ...
            }
        ]
    }
]

In short, your JSON is an array of objects , where an object consists of name-value pairs . 简而言之,您的JSON是一个对象 数组 ,其中一个对象名称-值对组成 In this case it looks like each of those objects consists of exactly one name-value pair, where the name appears to be a unique key of some sort and the value is an array of OutputList . 在这种情况下,看起来每个对象都仅由一个名称-值对组成,其中名称似乎是某种唯一键,而值是OutputList的数组。

How do you represent something in C# that maps a unique string-based key to an array of OutputList ? 您如何在C#中表示将唯一的基于字符串的键映射到OutputList数组的OutputList Using Dictionary<string, OutputList[]> . 使用Dictionary<string, OutputList[]> Then, your entire JSON is just an array of those dictionaries: 然后,您的整个JSON只是这些词典的数组:

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

This gives me a properly deserialized object without any nulls. 这给了我一个正确的反序列化对象,没有任何空值。 If it's not the structure you expected or wanted, then you'll need to either change the JSON, change your class structure, or transform the resulting array of dictionaries into eg just a single dictionary with multiple key-value pairs: 如果不是您期望或想要的结构,那么您将需要更改JSON,更改类结构或将生成的字典数组转换为例如带有多个键值对的单个字典:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM