简体   繁体   中英

Can't parse a JSON Response

I have a very simple response from a Json request, however I can't seem to find a easy way to parse it. I only find tutorials that use classes of third party's. I want to use the native functionality of .NET 3.5 writing in C# to interpret the response. Can anybody help, please?

{
    "id": "10000",
    "key": "TST-24",
    "self": "http://www.example.com/jira/rest/api/2/issue/10000"
}

You can use the JavaScriptSerializer , it is available for .net 3.5.

Consider to use the very popular and easy Json.NET which can be installed with nu-get.

You can do it natively, provided you define server-level matching counterpart for json object:

[DataContract]
public class MyObject {
  [DataMember]
  public string id { get; set; }
  [DataMember]
  public string key { get; set; }
  [DataMember]
  public string self { get; set; }
}

public T FromJson<T>(string value) {
  var serializer = new DataContractJsonSerializer(typeof(T));
  T result;
  using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(value), false)) {
    result = (T)serializer.ReadObject(stream);
  }
  return result;
}
var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(json);
Console.WriteLine(dict["id"] + " " + dict["key"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM