当字段名称包含空格时反序列化JSON [英] Deserializing JSON when fieldnames contain spaces

查看:322
本文介绍了当字段名称包含空格时反序列化JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个读取JSON文件的工具.我正在使用NewtonSoft工具将JSOn反序列化为C#类.这是一个示例片段:

I'm writing a tool to read JSON files. I'm using the NewtonSoft tool to deserialize the JSOn to a C# class. Here's an example fragment:

 "name": "Fubar",
 ".NET version": "4.0",
 "binding type": "HTTP",

字段名称包含空格和其他字符(.),这些字符在C#标识符中无效. 正确的方法是什么?

The field names contain spaces and other characters (the .) that are invalid in C# identifiers. What is the correct way to do this?

(很遗憾,我没有更改JSON格式的选项.)

(Unfortunately I don't have the option of changing the JSON format.)

推荐答案

使用JsonProperty属性在JSON中指示名称.例如

Use the JsonProperty attribute to indicate the name in the JSON. e.g.

[JsonProperty(PropertyName = "binding type")]
public string BindingType { get; set; }

这篇关于当字段名称包含空格时反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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