C#.net如何反序列化JSON的复杂对象
我有一个JSON字符串,我需要一些帮助来反序列化它。
没有什么对我有用..这是JSON
{ "response": [{ "loopa": "81ED1A646S894309CA1746FD6B57E5BB46EC18D1FAff", "drupa": "D4492C3CCE7D6F839B2BASD2F08577F89A27B4ff", "images": [{ "report": { "nemo": "unknown" }, "status": "rock", "id": "7e6ffe36e-8789e-4c235-87044-56378f08m30df", "market": 1 }, { "report": { "nemo": "unknown" }, "status": "rock", "id": "e50e99df3-59563-45673-afj79e-e3f47504sb55e2", "market": 1 } ] }] }
我有一个类的例子,但我不必使用这些类,我不介意使用其他类。 这些是类:
public class Report { public string nemo { get; set; } } public class Image { public Report report { get; set; } public string status { get; set; } public string id { get; set; } public int market { get; set; } } public class Response { public string loopa { get; set; } public string drupa{ get; set; } public Image[] images { get; set; } } public class RootObject { public Response[] response { get; set; } }
我想提一下,我已经有了Newtonsoft.Json,所以我可以使用那里的一些function。
我怎样才能做到这一点? 谢谢。
我在我的代码中使用这样的,它工作正常
下面是您需要编写的一段代码
using System.Web.Script.Serialization; JavaScriptSerializer oJS = new JavaScriptSerializer(); RootObject oRootObject = new RootObject(); oRootObject = oJS.Deserialize(Your JSon String);
应该是这样的:
var jobject = JsonConvert.DeserializeObject(jsonstring);
您可以将json字符串粘贴到此处: http : //json2csharp.com/以检查您的类是否正确。
如果您使用C#2010或更新版本,则可以使用动态类型 :
dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonstring);
然后,您可以使用点表示法访问动态对象中的属性和数组:
string nemo = json.response[0].images[0].report.nemo;
首先使用NuGet Package Manager
将newtonsoft.json
包安装到Visual Studio
然后添加以下代码:
ClassName ObjectName = JsonConvert.DeserializeObject < ClassName > (jsonObject);
我有一个场景,这个帮助了我
JObject
objParserd = JObject
.Parse(jsonString);
JObject
arrayObject1 =( JObject
)objParserd [“d”];
D
myOutput = JsonConvert
.DeserializeObject
(arrayObject1.ToString());
shareInfo是类:
public class ShareInfo { [JsonIgnore] public readonly DateTime Timestamp = DateTime.Now; [JsonProperty("sharename")] public string ShareName = null; [JsonProperty("readystate")] public string ReadyState = null; [JsonProperty("created")] [JsonConverter(typeof(Newtonsoft.Json.Converters.UnixDateTimeConverter))] public DateTime? CreatedUtc = null; [JsonProperty("title")] public string Title = null; [JsonProperty("getturl")] public string GettUrl = null; [JsonProperty("userid")] public string UserId = null; [JsonProperty("fullname")] public string Fullname = null; [JsonProperty("files")] public GettFile.FileInfo[] Files = new GettFile.FileInfo[0]; } // POST request. var gett = new WebClient { Encoding = Encoding.UTF8 }; gett.Headers.Add("Content-Type", "application/json"); byte[] request = Encoding.UTF8.GetBytes(jsonArgument.ToString()); byte[] response = gett.UploadData(baseUri.Uri, request); // Response. var shareInfo = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(response));
public static void Main(string[] args) { string json = @" { ""children"": [ { ""url"": ""foo.pdf"", ""expanded"": false, ""label"": ""E14288-Passive-40085-2014_09_26.pdf"", ""last_modified"": ""2014-09-28T11:19:49.000Z"", ""type"": 1, ""size"": 60929 } ] }"; var result = JsonConvert.DeserializeObject(json); DataTable tbl = DataTableFromObject(result.children); } public static DataTable DataTableFromObject(IList list) { DataTable tbl = new DataTable(); tbl.TableName = typeof(T).Name; var propertyInfos = typeof(T).GetProperties(); List columnNames = new List (); foreach (PropertyInfo propertyInfo in propertyInfos) { tbl.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType); columnNames.Add(propertyInfo.Name); } foreach(var item in list) { DataRow row = tbl.NewRow(); foreach (var name in columnNames) { row[name] = item.GetType().GetProperty(name).GetValue(item, null); } tbl.Rows.Add(row); } return tbl; } public class Child { public string url { get; set; } public bool expanded { get; set; } public string label { get; set; } public DateTime last_modified { get; set; } public int type { get; set; } public int size { get; set; } } public class ChildrenRootObject { public List children { get; set; } }
我使用以下:
using System.Web.Script.Serialization; ... public static T ParseResponse(string data) { return new JavaScriptSerializer().Deserialize (data); }
我解决了这个问题,为所有属性添加一个公共setter,应该反序列化。
您可以像下面的一堆代码一样解决您的问题
public class Response { public string loopa { get; set; } public string drupa{ get; set; } public Image[] images { get; set; } } public class RootObject { public List response{ get; set; } } var des = (RootObject)Newtonsoft.Json.JsonConvert.DeserializeObject(Your JSon String, typeof(RootObject ));