如何使用DataContractJsonSerializer对JSON? [英] How to use DataContractJsonSerializer for Json?

查看:84
本文介绍了如何使用DataContractJsonSerializer对JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JSON结构是这样的:

I have json structure like this:

{
标识:12345,
FIRST_NAME:恐龙,
姓氏:他,
电子邮件:{
首选:1,
个人化:2,
企业:3,
等:4
}

{ "id":"12345", "first_name": "dino", "last_name": "he", "emails": { "preferred": "1", "personal": "2", "business": "3", "other": "4" }

我想在电子邮件的价值
所以我写两个类:

I want to get the value in Emails So I write two class:

[DataContract]
public class UserInformation
{
    [DataMember(Name = "id")]
    public string ID { get; set; }
    [DataMember(Name = "emails")]
    public Emails emails { get; set; }
    [DataMember(Name = "last_name")]
    public string Name { get; set; }

}
[DataContract]
public class Emails
{
    [DataMember(Name = "preferred")]
    public string Preferred { get; set; }


    [DataMember(Name = "personal")]
    public string Account { get; set; }

    [DataMember(Name = "business")]
    public string Personal { get; set; }

    [DataMember(Name = "other")]
    public string Business { get; set; }
}


我写这样的代码:

And I write code like this:

StreamReader stream=new StreamReader(@"C:\Visual Studio 2012\Projects\ASP.net\WebApplication1\WebApplication2\TextFile1.txt");
            string text = stream.ReadToEnd();
            stream.Close();

            byte[] byteArray = Encoding.UTF8.GetBytes(text);
            MemoryStream stream1 = new MemoryStream(byteArray);


             DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(UserInformation));
             var info = serializer.ReadObject(stream1) as UserInformation;
             stream1.Close();

有关信息,我可以得到UserInformation其他值,但是对于我的电子邮件没有得到。
为什么,我应该怎么写的课吗?
请帮助我!

For info, I can get other value in UserInformation, But for Emails I get nothing. Why, And how should I write the class? Please help me!

推荐答案

我发现这个问题是我需要更改电子邮件为小写我的全部财产...
我不知道为什么。但它的工作。

I found the problem is I need to change all my property in Emails to lower case... I don't know why.. But it worked.

这篇关于如何使用DataContractJsonSerializer对JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆