Serialise JSON对象

时间:2013-12-22 13:25:18

标签: json vb.net

我以前曾问过这个问题,但是没有人能得到答案吗?下面的json字符串是使用jquery生成的,我需要使用vb.net生成相同的字符串我试图使用Json2csharp转换字符串(然后将其转换为vb.net)但是获取返回无效名称的类对象。你如何构建一个能够应对'$ gte'和'$ date'参数的类?

JSON:

{ "driver" : "Driver", "_createdAt":{"$gte":{"$date":"2013-11-01T11:36:35.151Z"}}} 

Json2csharp对话

public class Gte
{
    public string __invalid_name__$date { get; set; }
}

public class CreatedAt
{
    public Gte __invalid_name__$gte { get; set; }
}

public class RootObject
{
    public string driver { get; set; }
    public CreatedAt _createdAt { get; set; }
}

修改

嗨,感谢您的指示我现在已经获得了下面示例中返回JSON的代码:

{"driver":"JBOHEN","_createdAt":{"$gte":"2013-11-23T10:44:58.667Z"}}

我如何构造类,以便JSON包含日期运算符,如?

{"driver " : "Driver", "_createdAt":{"$gte":{"$date":"2013-11-01T11:36:35.151Z"}}} 

public class Gte
{
    [JsonProperty(PropertyName = "$date")]
    public string date { get; set; }
}

public class CreatedAt
{
    [JsonProperty(PropertyName = "$gte")]
    public string gte { get; set; }
}

public class RootObject
{
    public string driver { get; set; }
    public CreatedAt _createdAt { get; set; }
}

1 个答案:

答案 0 :(得分:0)

您可以使用 JsonPropertyAttribute

  

指示JsonSerializer始终使用指定的名称序列化成员。

您可以使用[JsonProperty]属性来装饰您希望控制其名称的属性,该属性允许您指定其他名称:

[JsonProperty(PropertyName = "$date")]
public string date { get; set; }

Official Docs

修改

将类构造为

public class Gte
{
    [JsonProperty(PropertyName = "$date")]
    public DateTime date { get; set; }
}

public class CreatedAt
{
    [JsonProperty(PropertyName = "$gte")]
    public Gte gte { get; set; }
}

public class RootObject
{
    public string driver { get; set; }
    public CreatedAt _createdAt { get; set; }
}