如何将复杂的JSON对象转换为CLR对象与json.net? [英] How do I convert a complex json object to CLR object with json.net?

查看:339
本文介绍了如何将复杂的JSON对象转换为CLR对象与json.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个愚蠢的问题抱歉,但我还没有发现这方面的任何解决方案。



下面是我的JSON:

  {
标识:1,
Parent.Id:1,
Agent.Id: 1,
Agent.Profile.FullName:GENA,
费:10.1200,
FeeManagementDate:29/11/2013​​,
Contact.Name:Genady,
Contact.Telephone:000000000,
Contact.Email:gena@email.com,
AgreementUrl :http://www.test.com/agreement
}

下面是我的目标。

 公共类ManagementDetailsViewModel:视图模型< INT> {
公共ManagementDetailsViewModel(){

}
公共字符串AgreementUrl {搞定;组; }


公共HttpPostedFileBase AgreementFile {搞定;组; }


公共小数费{搞定;组; } //支付数据

公众的DateTime? FeeDate {搞定;组; }


公共字符串FeeManagementDate {
{返回FeeDate!= NULL? FeeDate.Value.ToString(DD / MM / YYYY):DateTime.Now.ToString(DD / MM / YYYY); }
集合{
FeeDate = Convert.ToDateTime(值);
}
}

公共BusinessViewModel家长{搞定;组; }
公共MemberViewModel代理{搞定;组; }
公共联络联系{搞定;组; }
}



我如何 转换 JSON字符串对象(衬衣对象)?


解决方案

Json.Net需要一些帮助,因为你的JSON对象包含属性格式的名称,这是不是在C#一样( Agent.Id

  VAR OBJ = JsonConvert.DeserializeObject< MyObj中>(JSON); 







我如何转换的JSON字符串对象(内部对象)?




由于您的JSON是平的(不包含子对象),你必须张贴处理您的反序列化对象,如果你想用这种方式/






 公共类MyObj中
{
公共字符串ID {搞定;组; }

[JsonProperty(Parent.Id)]
公共字符串的ParentId {搞定;组; }

[JsonProperty(Agent.Id)]
公共字符串AGENTID {搞定;组; }

[JsonProperty(Agent.Profile.FullName)]
公共字符串ProfileFullName {搞定;组; }

公共字符串费{搞定;组; }

公共字符串FeeManagementDate {搞定;组; }

[JsonProperty(Contact.Name)]
公共字符串联系人姓名{搞定;组; }

[JsonProperty(Contact.Telephone)]
公共字符串ContactTelephone {搞定;组; }

[JsonProperty(Contact.Email)]
公共字符串ContactEmail {搞定;组; }

公共字符串AgreementUrl {搞定;组; }
}


sorry for a dumb question but I haven't found any solution for this.

Here is my JSON:

{
    "Id": "1",
    "Parent.Id": "1",
    "Agent.Id": "1",
    "Agent.Profile.FullName": "gena",
    "Fee": "10.1200",
    "FeeManagementDate": "29/11/2013",
    "Contact.Name": "Genady",
    "Contact.Telephone": "000000000",
    "Contact.Email": "gena@email.com",
    "AgreementUrl": "http://www.test.com/agreement"
}

Here is my object

 public class ManagementDetailsViewModel : ViewModel<int> {
    public ManagementDetailsViewModel() {

    }
    public string AgreementUrl { get; set; }


    public HttpPostedFileBase AgreementFile { get; set; }


    public decimal Fee { get; set; } // payment data

    public DateTime? FeeDate { get; set; }


    public string FeeManagementDate {
        get { return FeeDate != null ? FeeDate.Value.ToString("dd/MM/yyyy") : DateTime.Now.ToString("dd/MM/yyyy"); }
        set {
            FeeDate = Convert.ToDateTime(value);
        }
    }

    public BusinessViewModel Parent { get; set; }
    public MemberViewModel Agent { get; set; }
    public Contact Contact { get; set; }
}

How do I convert the json string to object (with inner objects)?

解决方案

Json.Net needs some help because your json object contain propery names, which is not valid in c# like(Agent.Id)

var obj  = JsonConvert.DeserializeObject<MyObj>(json);


How do I convert the json string to object (with inner objects)?

Since your json is flat(not containing sub objects) you have to post process your deserialized object if you want to use it that way/


public class MyObj
{
    public string Id { get; set; }

    [JsonProperty("Parent.Id")]
    public string ParentId { get; set; }

    [JsonProperty("Agent.Id")]
    public string AgentId { get; set; }

    [JsonProperty("Agent.Profile.FullName")]
    public string ProfileFullName { get; set; }

    public string Fee { get; set; }

    public string FeeManagementDate { get; set; }

    [JsonProperty("Contact.Name")]
    public string ContactName { get; set; }

    [JsonProperty("Contact.Telephone")]
    public string ContactTelephone { get; set; }

    [JsonProperty("Contact.Email")]
    public string ContactEmail { get; set; }

    public string AgreementUrl { get; set; }
}

这篇关于如何将复杂的JSON对象转换为CLR对象与json.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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