简体   繁体   English

将json反序列化为c#对象

[英]deserializing json to c# object

my C# is not well but I want to deserializing this my json file to C# object: 我的C#是不是很好,但我想这反序列化JSON我的文件,以C#对象:

[
{
    "command":"",
    "name":"eee",
    "children":
    [
        {
            "command":"Report",
            "name":"x",
            "children":[],
            "path":"wwwwww",
            "params":
            {
                "PeriodType":"1i",
                "District":"0i"
            }
        },...
    ],
    "path":"",
    "params":{}
},...

for this schema I have created this object: 对于这个模式,我创建了这个对象:

[DataContract]
public class ListCommands
{
    [DataMember]
    public List<Commands> commandList { get; set; }
    [DataContract]
    public class Commands
    {
        [DataMember]
        public string command { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string path { get; set; }
        [DataMember(Name = "params")]
        public Params parameters { get; set; }
        [DataMember]
        public List<Commands> children { get; set; }
    }
}
}

and : 和:

public class Params
{
    [DataMember]
    public string PeriodType { get; set; }
    [DataMember]
    public string District { get; set; }
}
}

and I am using this code for deserializing json to c# object: 而且我正在使用此代码将json反序列化为c#对象:

public static void ReadJsonFile()
    {
        ListCommands comList = new ListCommands();
        //List<Commands> comList = new List<Commands>();
        string root = HttpContext.Current.Server.MapPath("~/File");
        using (FileStream stream = File.OpenRead(root + "\\commands.json"))
            comList  = (ListCommands)new DataContractJsonSerializer(typeof(ListCommands)).ReadObject(stream);
    }
}

but unfortunately I got this error: 但是不幸的是我得到了这个错误:

Additional information: There was an error deserializing the object of type Notifications.Contracts.ListCommands. Encountered unexpected character 'ï'.

Where is the problem?I have a json file and I want to read this file and then convert to the c# object. 问题出在哪里?我有一个json文件,我想读取此文件然后转换为c#对象。

According suggestion of @SirRufo 根据@SirRufo的建议

1- I have used of Json.Net in this way: 1 -我已经使用这种方式Json.Net的:

string root = HttpContext.Current.Server.MapPath("~/File");
FileStream stream = File.OpenRead(root + "\\commands.json");
StreamReader reader = new StreamReader(stream);
var comList = JsonConvert.DeserializeObject<dynamic>(reader.ReadToEnd());

and above error gone. 以上错误消失了。

2- By this link I could run previously code: enter link description here 2-通过此链接,我可以运行以前的代码: 在此处输入链接说明

One of the reasons for this could be that the input file that contains the JSON-encoded data is created with a binary encoding or it has a Byte Order Mark(BOM) byte sequence for a binary encoded file.
For e.g. The UTF-8 representation of the BOM is the byte sequence (0xEF,0xBB,0xBF) in the beginning of the file.
**Note:** You will see this if you created a .JSON file(or a binary file) using visual studio. 

The data format for deserialization differs. 反序列化的数据格式不同。

  1. Change json data. 更改json数据。

    { "commandList":[ { "command":"", "name":"eee", "children": [ { "command":"Report", "name":"x", "children":[], "path":"wwwwww", "params": { "PeriodType":"1i", "District":"0i" } } ], "path":"", "params":{} }] } {“ commandList”:[{“ command”:“”,“ name”:“ eee”,“ children”:[{“ command”:“ Report”,“ name”:“ x”,“ children”:[] “路径”: “wwwwww”, “PARAMS”:{ “PeriodType”: “1I”, “区”: “0I”}}], “路径”: “”, “PARAMS”:{}}]}

or 2. Change the deserialization target type 或2.更改反序列化目标类型

comList.commandList = (List<ListCommands.Commands>)new DataContractJsonSerializer(typeof(List<ListCommands.Commands>)).ReadObject(stream);

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

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