tug*_*erk 33 c# json javascriptserializer deserialization
我将反序列化的json文件的结构如下所示;
{
"id" : "1lad07",
"text" : "test",
"url" : "http:\/\/twitpic.com\/1lacuz",
"width" : 220,
"height" : 84,
"size" : 8722,
"type" : "png",
"timestamp" : "Wed, 05 May 2010 16:11:48 +0000",
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个类,其字段名称为JavaScriptSerializer的属性.我将用于反序列化json的代码如下:
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
var responseBody = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Response>(responseBody);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何读取json文件上的用户字段.这就像下面;
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
Run Code Online (Sandbox Code Playgroud)
它有子属性和值.我怎样才能在我的Response类中命名它们.我的回复课现在看起来像这样;
public class Response {
public string id { get; set; }
public string text { get; set; }
public string url { get; set; }
public string width { get; set; }
public string height { get; set; }
public string size { get; set; }
public string type { get; set; }
public string timestamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这样做最好的情况是什么?
ntz*_*lis 36
User.使用用户值的新类的类型向Response类'user'添加属性User.
public class Response {
public string id { get; set; }
public string text { get; set; }
public string url { get; set; }
public string width { get; set; }
public string height { get; set; }
public string size { get; set; }
public string type { get; set; }
public string timestamp { get; set; }
public User user { get; set; }
}
public class User {
public int id { get; set; }
public string screen_name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)通常,您应该确保json和CLR类的属性类型匹配.您尝试反序列化的结构似乎包含多个数字值(最有可能int).我不确定是否JavaScriptSerializer能够自动将数字反序列化为字符串字段,但您应该尽可能地将CLR类型与尽可能接近实际数据相匹配.
ska*_*ats 19
假设您不想创建另一个类,您始终可以让反序列化器为您提供键值对的字典,如下所示:
string s = //{ "user" : { "id" : 12345, "screen_name" : "twitpicuser"}};
var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(s);
Run Code Online (Sandbox Code Playgroud)
你会得到一些东西,你可以做到:
var userId = int.Parse(result["user"]["id"]); // or (int)result["user"]["id"] depending on how the JSON is serialized.
// etc.
Run Code Online (Sandbox Code Playgroud)
看看result在调试器中看到,里面有什么了.
对于.Net 4+:
string s = "{ \"user\" : { \"id\" : 12345, \"screen_name\" : \"twitpicuser\"}}";
var serializer = new JavaScriptSerializer();
dynamic usr = serializer.DeserializeObject(s);
var UserId = usr["user"]["id"];
Run Code Online (Sandbox Code Playgroud)
对于.Net 2/3.5:此代码应适用于1级JSON
samplejson.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%
string s = "{ \"id\" : 12345, \"screen_name\" : \"twitpicuser\"}";
var serializer = new JavaScriptSerializer();
Dictionary<string, object> result = (serializer.DeserializeObject(s) as Dictionary<string, object>);
var UserId = result["id"];
%>
<%=UserId %>
Run Code Online (Sandbox Code Playgroud)
对于2级JSON:
sample2.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%
string s = "{ \"user\" : { \"id\" : 12345, \"screen_name\" : \"twitpicuser\"}}";
var serializer = new JavaScriptSerializer();
Dictionary<string, object> result = (serializer.DeserializeObject(s) as Dictionary<string, object>);
Dictionary<string, object> usr = (result["user"] as Dictionary<string, object>);
var UserId = usr["id"];
%>
<%= UserId %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
124308 次 |
| 最近记录: |