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

问题描述:

我想使用Newtonsoft.Json库解析一些JSON。文件似乎有点稀疏,我很困惑如何完成我所需要的。这是我需要解析的JSON格式。c#如何将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>)); 

但我总是得到一个错误。有任何想法吗?

编辑: 我得到一个错误:

Newtonsoft.Json.JsonSerializationException: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[ConsoleApplication1.Foo]' (...) 
+2

什么是错误? –

+0

Newtonsoft.Json.JsonSerializationException:无法将JSON对象反序列化为类型'System.Collections.Generic.List'1 [ConsoleApplication1.Foo]'(...) –

+0

您不能使用任何外部库。你可以阅读这篇文章http://*.com/questions/5879951/c-json-net-how-can-i-deserialized-the-message/5882057#5882057 –

这是丑了一点,但可能适合您的需求。它也不使用第三方解决方案。道歉,如果它有点kludgey!

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

} 

样品:

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

OUTPUT:

error:0 
data:type any data(1674), size:45678 
data:type any data(1256), size:12345