序列化JSON对象

序列化JSON对象,json,vb.net,Json,Vb.net,我以前问过这个问题,但没有得到答案。有人能帮忙吗?下面的json字符串是使用jquery生成的,我需要使用vb.net生成相同的字符串。我尝试使用Json2csharp转换该字符串,然后将其转换为vb.net,但返回了具有无效名称的类对象。如何构造一个处理“$gte”和“$date”参数的类 JSON: JSON2C尖锐的对话 public class Gte { public string __invalid_name__$date { get; set; } } public cl

我以前问过这个问题,但没有得到答案。有人能帮忙吗?下面的json字符串是使用jquery生成的,我需要使用vb.net生成相同的字符串。我尝试使用Json2csharp转换该字符串,然后将其转换为vb.net,但返回了具有无效名称的类对象。如何构造一个处理“$gte”和“$date”参数的类

JSON:

JSON2C尖锐的对话

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; }
}
您可以使用JsonPropertyAttribute

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

您可以使用[JsonProperty]属性装饰您想要控制其名称的属性,该属性允许您指定不同的名称:

[JsonProperty(PropertyName = "$date")]
public string date { get; set; }
编辑 将类构造为

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; }
}

如果你愿意的话。您可以尝试JSONPropertyAttribute。我使用JSON.Net进行基本操作序列化、反序列化。我查看了文档,但找不到任何使用JSONPropertyAttribute的示例。您有任何示例吗?嗨,Satpal,谢谢您,我现在得到了正确语法的JSON。@user1898898,很高兴我能提供帮助:嗨,我用'[JsonPropertyname'但vs2012无法识别“[JsonProperty”,它显示“括号标识符缺少结束括号]”,如何修复此问题?@f1wade,您需要使用[JsonPropertyname],您缺少结束符]
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; }
}