簡體   English   中英

C# JSON 匿名類型 - 具有相同名稱的多個屬性

[英]C# JSON Anonymous Type - Multiple properties with the same name

我需要用 C# 生成這個 JSON 字符串:

{
  "in0": {
    "children": [
      {
        "ValueObjectDescriptor": {
          "fields": [
            {
              "FieldDescriptor": {
                "name": "length",
                "xpath": "@lenth"
              }
            },
            {
              "FieldDescriptor": {
                "name": "height",
                "xpath": "@height"
              }
            },
            {
              "FieldDescriptor": {
                "name": "width",
                "xpath": "@width"
              }
            }
          ],
          "objectName": "Job",
          "limit": 1,
          "xpathFilter": "@openJob = 'true'"
        }
      }
    ]
  }
}

這是我的代碼:

static string BuildJsonString()
        {
            var json = new
            {
                in0 = new
                {
                    children = new
                    {
                        ValueObjectDescriptor = new
                        {
                            fields = new
                            {
                                FieldDescriptor = new
                                {
                                    name = "length",
                                    xpath = "@length",
                                },
                                FieldDescriptor = new
                                {
                                    name = "height",
                                    xpath = "@height",
                                },
                                FieldDescriptor3 = new
                                {
                                    name = "width",
                                    xpath = "@width",
                                },
                                objectName = "Job",
                                limit = "1",
                                xpathFilter = "@openJob='true'"
                            }
                        }
                    }
                }
            };

            var jsonFormatted = JsonConvert.SerializeObject(json, Newtonsoft.Json.Formatting.Indented);

            return jsonFormatted.ToString();

我遇到的問題是編譯器不喜歡我多次使用“FieldDescriptor”,我收到錯誤“匿名類型不能有多個同名的屬性”。

我對 JSON 非常陌生,因此將不勝感激任何建議。

請注意,不僅具有相同名稱的多個字段無效的 C# 代碼,在相同的 object 中具有重復鍵也是無效的 JSON。 您可以確定沒有 JSON 需要您編寫重復的字段名稱來對其進行序列化。

The "FieldDescriptor" JSON keys are actually part of different JSON objects, and these JSON objects are all in a JSON array under the key "fields":

[
  {
    "FieldDescriptor": {
      "name": "length",
      "xpath": "@lenth"
    }
  },
  {
    "FieldDescriptor": {
      "name": "height",
      "xpath": "@height"
    }
  },
  {
    "FieldDescriptor": {
      "name": "width",
      "xpath": "@width"
    }
  }
]

[... ]表示數組,每對{... }表示一個 JSON object。 因此,您應該創建一個(隱式類型)匿名對象數組,每個對象都具有FieldDescriptor屬性,而不是一個object 具有所有三個屬性:

fields = new[] // <--- create an array
{
   new { 
   FieldDescriptor = new
   {
       name = "length",
       xpath = "@length",
   }},
   new { // notice the new pairs of curly braces
   FieldDescriptor = new
   {
       name = "height",
       xpath = "@height",
   }}, // here's the closing brace
   new {
   FieldDescriptor3 = new
   {
       name = "width",
       xpath = "@width",
   }},
   objectName = "Job",
   limit = "1",
   xpathFilter = "@openJob='true'"
}

看起來在 json 中,“字段”是一個對象數組,其中每個 object 都包含一個“字段描述符”字段。 在您的 C# 代碼中,您正在創建“字段”作為 object 具有多個字段而不是對象數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM