使用 JavaScriptSerializer() 反序列化 JSON 文件
我将反序列化的 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"
}
}
我创建了一个类,其中包含文件名作为 JavaScriptSerializer 的属性。我将用来反序列化 json 的代码如下;
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
var responseBody = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Response>(responseBody);
}
我的问题是如何读取 json 文件上的用户字段。如下所示;
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
它有子属性和值。我如何在我的响应类中命名它们。我的响应类现在看起来像这样;
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; }
}
最好的情况是什么?
the json file's structure which I will deserialize looks like below;
{
"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"
}
}
I have created a class which has the filed names as properties for JavaScriptSerializer. The code which I will use to Deserialize the json is as follows;
using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {
var responseBody = reader.ReadToEnd();
var deserializer = new JavaScriptSerializer();
var results = deserializer.Deserialize<Response>(responseBody);
}
My problem is how I can read the user field on json file. which is like below;
"user" : {
"id" : 12345,
"screen_name" : "twitpicuser"
}
it has sub properties and values. how can I name them on my Response class. my response class now look like this;
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; }
}
what is the best case to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
User
一样。向响应类“user”添加一个属性,其类型为用户值
User
的新类的类型。一般来说,您应该确保 json 的属性类型与您的 CLR 类匹配。 您尝试反序列化的结构似乎包含多个数值(很可能是
int< /代码>)。我不确定 JavaScriptSerializer 是否能够自动将数字反序列化为字符串字段,但无论如何,您都应该尝试将 CLR 类型与实际数据尽可能匹配。
User
.Add a property to the Response class 'user' with the type of the new class for the user values
User
.In general you should make sure the property types of the json and your CLR classes match up. It seems that the structure that you're trying to deserialize contains multiple number values (most likely
int
). I'm not sure if theJavaScriptSerializer
is able to deserialize numbers into string fields automatically, but you should try to match your CLR type as close to the actual data as possible anyway.假设您不想创建另一个类,您始终可以让反序列化器为您提供一个键值对字典,如下所示:
您将返回一些内容,您可以在其中执行以下操作:
查看
result< /code> 在调试器中查看其中有什么。
Assuming you don't want to create another class, you can always let the deserializer give you a dictionary of key-value-pairs, like so:
You'll get back something, where you can do:
Look at
result
in the debugger to see, what's in there.对于 .Net 4+:
对于 .Net 2/3.5:
此代码应该适用于 1 级的 JSON
Samplejson.aspx
以及 2 级的 JSON:
sample2.aspx
For .Net 4+:
For .Net 2/3.5:
This code should work on JSON with 1 level
samplejson.aspx
And for a 2 level JSON:
sample2.aspx
创建一个带有 id 字段和 screen_name 字段的子类 User,如下所示:
Create a sub-class User with an id field and screen_name field, like this: