简体   繁体   English

JSON - 在 c# 中添加新类型或从特定成员值中删除引号

[英]JSON - Add new type in c# or remove quotes from a particular member value

I am trying to remove the quotes from a particular member value from the JSON.我正在尝试从 JSON 中删除特定成员值的引号。

I need the JSON to be:我需要 JSON 为:
"editor": TextAreaEditor “编辑器”:文本区域编辑器
Not:不是:
"editor": "TextAreaEditor" “编辑器”:“文本区域编辑器”

I need to be able to deserialize JSON into an object and serialize back to JSON我需要能够将 JSON 反序列化为 object 并序列化回 JSON

I am basically deserializng the below JSON and then serializing back into JSON for my c# web api. I am basically deserializng the below JSON and then serializing back into JSON for my c# web api.

configList = JsonConvert.DeserializeObject<List<ReportJsonDetails>>(json, settings);

I get this我明白了

Unexpected character encountered while parsing value: T. Path
 '[0].headers[2].editor'

I saw a bunch of examples using JsonConverter.我看到了一堆使用 JsonConverter 的例子。 But most of the examples are manipulating the values and I doesnt seem like booleans, nulls and numbers are implemented that way.但是大多数示例都在操作值,我似乎不像布尔值、空值和数字那样实现。

JSON JSON

[   {
    "name": "DataviewReport",
    "headers": [
      {
        "id": "id",
        "caption": "Id",
        "dataField": "id",
        "visible": false
      },
      {
        "id": "lastname",
        "caption": "Last Name",
        "dataField": "lastname",
        "visible": true
      },
      {
        "id": "FirstName",
        "caption": "First Name",
        "dataField": "levelNum",
        "visible": true,
        "editor": TextAreaEditor
      }
    ]   } ]
      {
        "id": "FirstName",
        "caption": "First Name",
        "dataField": "levelNum",
        "visible": true,
        "editor": TextAreaEditor
      }

When you see this representation in something like your development tools, this isn't JSON.当您在开发工具之类的东西中看到这种表示时,这不是 JSON。 This is a JSON-like representation of the memory model of the JavaScript environment.这是 JavaScript 环境的 memory model 的类似 JSON 的表示。 TextAreaEditor is a concept that exists in that JavaScript environment, which doesn't translate universally to other environments. TextAreaEditor是存在于 JavaScript 环境中的一个概念,它不能普遍转换为其他环境。

JSON is meant to be a serialization language that translates well into JavaScript, but it's not the same as the JavaScript object model itself. JSON is meant to be a serialization language that translates well into JavaScript, but it's not the same as the JavaScript object model itself. If you have JavaScript objects containing values that aren't inherently serializable, then they cannot be included in JSON and retain the same meaning.如果您的 JavaScript 对象包含本质上不可序列化的值,则它们不能包含在 JSON 中并保留相同的含义。

尝试将 JSON 字符串化为不可序列化的模型

So there isn't (and shouldn't be) a way to force C# to deserialize the value you're giving it, because it's not valid JSON.所以没有(也不应该)强制 C# 反序列化你给它的值,因为它不是有效的 JSON。 The solution to your problem is to re-think what data you actually need to transfer from your client to your server, and create a translation layer between that data model and the one your client is using directly: a so-called Data Transfer Object.您的问题的解决方案是重新考虑您实际需要从客户端传输到服务器的数据,并在该数据 model 和您的客户端直接使用的数据之间创建一个转换层:所谓的数据传输 Object。 Then serialize that.然后将其序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM