在我的c#示例中解释JSON结构

时间:2013-10-08 10:15:13

标签: c# json

我有这种json结构:

      {
       "Root": {
           "data": [
               {
                   "CardName": "card1",
                   "functions": [
                       {
                           "State": "OPEN",
               "State": "INHERENT"
                       }
                   ]
               },
               {
                   "CardName": "card2",
                   "functions": [
                       {
                           "State": "CLOSED",
               "State": "INHERENT"
                       }
                   ]
               }
           ]
       }
   }

我的C#课程是:

    [DataContract]
public class Card
{
    [DataMember(Name = "CardName")]
    public string CardName { get; set; }

    [DataMember(Name = "functions")]
    public List<Function> Functions { get; set; }
}

[DataContract]
public class Function
{

    [DataMember(Name = "State")]
    public string State { get; set; }
}

我想解析该结构以获取卡片列表,并且每张卡片都包含一系列功能。

此刻我正在尝试这个:

        string content = string.Empty;
        using (StreamReader sr = new StreamReader("json"))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                content += line;
            }
        }

   List<Card> dynObj = JsonConvert.DeserializeObject<Card>(content);

但我只得到一个空值列表。你能告诉我问题出在哪里吗?

5 个答案:

答案 0 :(得分:8)

通过在Visual Studio中粘贴JSON(编辑&gt;选择性粘贴 - >将JSON粘贴为类),它告诉我数据的类应该如下所示。

public class Rootobject
{
    public Root Root { get; set; }
}

public class Root
{
    public Datum[] data { get; set; }
}

public class Datum
{
    public string CardName { get; set; }
    public Function[] functions { get; set; }
}

public class Function
{
    public string State { get; set; }
}

答案 1 :(得分:2)

来自http://json2csharp.com/

public class Function
{
    public string State { get; set; }
}

public class Datum
{
    public string CardName { get; set; }
    public List<Function> functions { get; set; }
}

public class Root
{
    public List<Datum> data { get; set; }
}

public class RootObject
{
    public Root Root { get; set; }
}

答案 2 :(得分:1)

请按照以下更改您的jsona并尝试

   "Root": {
    "data": [{
        "CardName": "card1",
        "functions": [{
            "State": "OPEN"
        }, {
            "State": "INHERENT"
        }]
    }, {
        "CardName": "card2",
        "functions": [{
            "State": "CLOSED"
        }, {
            "State": "INHERENT"
        }]
    }]
}

答案 3 :(得分:1)

假设缺少的课程为:

public class Article
{
    public List<Card> Root { get; set; }
}

JSON中有一个错误,它应该是:

{
   "Root": [
           {
               "CardName": "card1",
               "functions": [
                   {
                       "State": "OPEN",
                   },

                   {
                       "State": "INHERENT"
                   }
               ]
           },
           {
               "CardName": "card2",
               "functions": [
                   {
                       "State": "CLOSED"
                   },
                   {
                       "State": "INHERENT"
                   }
               ]
           }
       ]

请注意,每个函数都应该在{}中,而根类应该包含一个卡列表。

或者,您可以反序列化为List,然后跳过包装“Root”。

答案 4 :(得分:1)

代码存在许多问题。我在下面的代码中解决了这些问题:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.Xml;
using ConsoleDemo.Controller;
using ConsoleDemo.Model;
using Microsoft.Practices.Unity;


namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = @"{""Root"": {""data"": [{""CardName"": ""card1"",""functions"": [{""State"": ""OPEN""},{""State"": ""INHERENT""}]},{""CardName"": ""card2"",""functions"": [{""State"": ""CLOSED""},{""State"": ""INHERENT""}]}]}";
            RootClass dynObj = JsonHelper.JsonDeserialize<RootClass>(data); //Get the object
            Console.ReadKey();
        }
    }

    [DataContract]
    public class RootClass
    {
        [DataMember(Name = "Root")]
        public Data Root { get; set; }
    }

    [DataContract]
    public class Data
    {
        [DataMember(Name = "data")]
        public List<Card> data { get; set; }
    }

    [DataContract]
    public class Card
    {
        [DataMember(Name = "CardName")]
        public string CardName { get; set; }

        [DataMember(Name = "functions")]
        public List<Function> Functions { get; set; }
    }

    [DataContract]
    public class Function
    {

        [DataMember(Name = "State")]
        public string State { get; set; }
    }

    public class JsonHelper
    {
        /// <summary>
        /// JSON Serialization
        /// </summary>
        public static string JsonSerializer<T>(T t)
        {
            var ser = new DataContractJsonSerializer(typeof(T));
            var ms = new MemoryStream();
            ser.WriteObject(ms, t);
            var jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
        /// <summary>
        /// JSON Deserialization
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            var ser = new DataContractJsonSerializer(typeof(T));
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            var obj = (T)ser.ReadObject(ms);
            return obj;
        }
    }

}