Deserialize JSON object to C# object

余生颓废 提交于 2019-12-05 04:45:22

问题


Im trying to read some json data using RestSharp.

But im having some problems reading json objects. I have this respons:

expand: "html",
self: "<url>/INCIDENT-447",   
key: "INCIDENT-447",
fields: {
  customfield_11414: {
  name: "Corrective Measures",
  type: "com.atlassian.jira.plugin.system.customfieldtypes:textarea"
},
  summary: {
  name: "summary",
  type: "java.lang.String",
  value: "BLA BLA BLA"
},  

I need to create a object with Property's summery and customfield_11414 But i only need the value of them. Not the entire JSON object


回答1:


You can use Json.Net and dynamic keyword together

dynamic dynObj = JsonConvert.DeserializeObject(json);
Console.WriteLine(dynObj.fields.customfield_11414.name + " " + 
                  dynObj.fields.summary.value);



回答2:


You have a few options. One is to not try and deserialise everything, but rather just make the JSON available for LINQ/XPATH style searching. This is using Json.NET:

var json = " ... "; // your json here
var o = JObject.Parse(json);
var summary = o["summary"];
var customfield_11414 = o.SelectToken("customfield_11414");

These return everything as JToken, which you could cast if needed, or further parse.



来源:https://stackoverflow.com/questions/10136049/deserialize-json-object-to-c-sharp-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!