将json反序列化为c#对象

时间:2017-12-02 11:46:35

标签: c# json

我的C#不好但是我想将我的json文件反序列化为C#对象:

List

对于这个模式,我创建了这个对象:

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

和:

[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; }
    }
}
}

我正在使用此代码将json反序列化为c#object:

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

但不幸的是我收到了这个错误:

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);
    }
}

问题出在哪里?我有一个json文件,我想读取这个文件,然后转换为c#对象。

2 个答案:

答案 0 :(得分:0)

根据 @SirRufo

的建议

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());

以上错误消失了。

2 - 通过此链接,我可以运行以前的代码: enter link description here

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. 

答案 1 :(得分:0)

反序列化的数据格式不同。

  1. 更改json数据。

    { &#34; commandList&#34;:[ {     &#34;命令&#34;:&#34;&#34 ;,     &#34;名称&#34;:&#34; EEE&#34 ;,     &#34;子&#34 ;:     [         {             &#34;命令&#34;:&#34;报告&#34 ;,             &#34;名称&#34;:&#34; X&#34 ;,             &#34;的子&#34;:[],             &#34;路径&#34;:&#34; wwwwww&#34 ;,             &#34; PARAMS&#34 ;:             {                 &#34; PeriodType&#34;:&#34; 1I&#34 ;,                 &#34;区&#34;:&#34; 0I&#34;             }         }     ]     &#34;路径&#34;:&#34;&#34 ;,     &#34; PARAMS&#34;:{} }] }

  2. 或2.更改反序列化目标类型

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