2

我有这种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);

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

4

5 回答 5

8

通过在 Visual Studio 中粘贴 JSON(编辑 > 选择性粘贴 -> 将 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; }
}
于 2013-10-08T10:29:27.800 回答
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; }
}
于 2013-10-08T10:28:05.677 回答
1

请如下更改您的 jsona 并尝试

   "Root": {
    "data": [{
        "CardName": "card1",
        "functions": [{
            "State": "OPEN"
        }, {
            "State": "INHERENT"
        }]
    }, {
        "CardName": "card2",
        "functions": [{
            "State": "CLOSED"
        }, {
            "State": "INHERENT"
        }]
    }]
}
于 2013-10-08T10:28:32.397 回答
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"
                   }
               ]
           }
       ]

请注意,每个函数都应位于 {} 中,并且根类应包含卡片列表。

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

于 2013-10-08T10:32:57.980 回答
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;
        }
    }

}
于 2013-10-08T10:47:48.397 回答