[英]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. 我从Json请求得到一个非常简单的响应,但是我似乎找不到一种简单的方法来解析它。 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. 我想使用用C#编写的.NET 3.5的本机功能来解释响应。 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. 您可以使用JavaScriptSerializer ,它适用于.net 3.5。
Consider to use the very popular and easy Json.NET which can be installed with nu-get. 考虑使用可以通过nu-get安装的非常流行且简单的Json.NET 。
You can do it natively, provided you define server-level matching counterpart for json object: 如果您为json对象定义了服务器级别的匹配对象,则可以本地完成:
[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"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.