1

I have used json2csharp to produce some nice c# class into which I can deserialize json.

It is actually working as expected EXCEPT that json2csharp named some of the fields invalid_name . I renamed those to valid csharp names but when serialized those class are null.

I found this other SO post... where one of the answerers said the following...

Keep in mind the class I have pasted below will not work directly, because of the naming of some of the fields in the json. You may have to rename them manually and map them.

This exactly describes my problem. Unfortunately, the answer gives no clues on actually HOW to "map them." So can someone tell me how to manually map some json arrays to c# classes.

I am using RestSharp deserializers, btw.

Any ideas?

4

1 回答 1

6

如果您使用 JSON.NET 作为基础或 JSON 解析,您可以重命名您的属性,然后用属性装饰它们以将它们与原始 JSON 对象对齐。

例如,JSON 中名为 1 的属性在 C# 中无效。这可以使用以下方法纠正:

 [JsonProperty("1")]
 public int? One { get; set; }

如果要在类级别进行编辑,还有一个 JsonObject 属性,还有一个 JsonIgnore 对象来忽略属性的序列化。

于 2013-02-19T04:28:18.427 回答