C# c如何将Json数据转换为对象

C# c如何将Json数据转换为对象,c#,json,deserialization,C#,Json,Deserialization,我正在尝试使用Newtonsoft.JSON库解析一些JSON。文档似乎有点稀疏,我对如何完成我需要的东西感到困惑。下面是我需要解析的JSON格式 const string json = @"{ ""error"" : ""0"", ""result"" : { ""1256"" : { ""data"" : ""type any data"", ""size"" : ""12345"" }, ""1674"" : { ""data"

我正在尝试使用Newtonsoft.JSON库解析一些JSON。文档似乎有点稀疏,我对如何完成我需要的东西感到困惑。下面是我需要解析的JSON格式

const string json = @"{   ""error"" : ""0"", 
""result"" :
{
   ""1256"" : {
      ""data"" : ""type any data"", 
      ""size"" : ""12345""
   },
   ""1674"" : {
      ""data"" : ""type any data"", 
      ""size"" : ""12345""
   },
   // ... max - 50 items
}
}";
我正在尝试将JSON数据转换为某种好的对象。这是我的班级:

public class ListLink
{
    public String data{ get; set; }
    public String size { get; set; }
}

public class SubResult
{
    public ListLink attributes { get; set; }
}

public class Foo
{
    public Foo() { results = new List<SubResult>(); }
    public String error { get; set; }
    public List<SubResult> results { get; set; }
}
.....
List<Foo> deserializedResponse =  
    (List<Foo>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Foo>));
样本:


这有点难看,但可能适合你的需要。它也不使用第三方解决方案。道歉,如果它有点笨拙

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

const string json = @"{   ""error"" : ""0"", 
                            ""result"" :
                            {
                               ""1256"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""123456""
                               },
                               ""1674"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""654321""
                               },
                               ""1845"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""432516""
                               },
                                ""1956"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""666666""
                               }
                            }
                            }";

JavaScriptSerializer j = new JavaScriptSerializer();

var x = (Dictionary<string, object>)j.DeserializeObject(json);

Foo foo = new Foo();

foo.error = x["error"].ToString();

foreach (KeyValuePair<string, object> item in (Dictionary<string, object>)x["result"])
{

    SubResult result = new SubResult();

    ListLink listLink = new ListLink();

    var results = (Dictionary<string, object>)item.Value;

    foreach (KeyValuePair<string, object> sub in results)
    {

        listLink.data = results.First().Value.ToString();
        listLink.size = results.Last().Value.ToString();

    }

    SubResult subResult = new SubResult { attributes = listLink };

    foo.results.Add(subResult);

}

Newtonsoft.Json.JsonSerializationException:无法将Json对象反序列化为“System.Collections.Generic.List`1[ConsoleApplication1.Foo]”类型……您不能使用任何外部库。你可以读这篇文章
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public struct Element {
    public string data{ get; set; }
    public int size { get; set; }
}

class Sample {
    static public void Main(){
        const string json =
        @"{ ""error"" : ""0"", 
            ""result"" :
            {
               ""1256"" : {
                  ""data"" : ""type any data(1256)"", 
                  ""size"" : ""12345""
               },
               ""1674"" : {
                  ""data"" : ""type any data(1674)"", 
                  ""size"" : ""45678""
               },
            }
        }";
        dynamic obj =  JsonConvert.DeserializeObject(json);
        int error = obj.error;
        Console.WriteLine("error:{0}", error);
        dynamic result = obj.result;
        Dictionary<string, Element> dic = new Dictionary<string, Element>();
        foreach(dynamic x in result){
            dynamic y = x.Value;
            dic[x.Name] = new Element { data = y.data, size = y.size};
        }
        //check
        Element el = dic["1674"];
        Console.WriteLine("data:{0}, size:{1}", el.data, el.size);
        el = dic["1256"];
        Console.WriteLine("data:{0}, size:{1}", el.data, el.size);
    }
}
error:0
data:type any data(1674), size:45678
data:type any data(1256), size:12345
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

const string json = @"{   ""error"" : ""0"", 
                            ""result"" :
                            {
                               ""1256"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""123456""
                               },
                               ""1674"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""654321""
                               },
                               ""1845"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""432516""
                               },
                                ""1956"" : {
                                  ""data"" : ""type any data"", 
                                  ""size"" : ""666666""
                               }
                            }
                            }";

JavaScriptSerializer j = new JavaScriptSerializer();

var x = (Dictionary<string, object>)j.DeserializeObject(json);

Foo foo = new Foo();

foo.error = x["error"].ToString();

foreach (KeyValuePair<string, object> item in (Dictionary<string, object>)x["result"])
{

    SubResult result = new SubResult();

    ListLink listLink = new ListLink();

    var results = (Dictionary<string, object>)item.Value;

    foreach (KeyValuePair<string, object> sub in results)
    {

        listLink.data = results.First().Value.ToString();
        listLink.size = results.Last().Value.ToString();

    }

    SubResult subResult = new SubResult { attributes = listLink };

    foo.results.Add(subResult);

}