将json对象转换为json数组c# [已关闭]

dwbf0jvd  于 2023-05-19  发布在  C#
关注(0)|答案(2)|浏览(89)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

3小时前关闭
Improve this question
下面是JSON格式。我想把图片做成json对象到json数组。表示要将json对象转换为json数组。
这是我当前的JSON

"data": [
        {
            "id": "testid",
            "variants": [
                {
                    "name": "testname",
                    "description": "testdesc",
                    "images": {
                        "2T": [
                            {
                                "id": testid,
                                "product_id": "testid1"
                            }
                        ]
                    }
                }
            ],
        }
        ]

我想让JSON看起来像这样。

"data": [
        {
            "id": "testid",
            "variants": [
                {
                    "name": "testname",
                    "description": "testdesc",
                    "images": [ // here I want to convert to json array 
                        "2T": [
                            {
                                "id": testid,
                                "product_id": "testid1"
                            }
                        ]
                    ]
                }
            ],
        }
        ]
s1ag04yj

s1ag04yj1#

我明白你的意思了您可能希望在C#中将JSON对象转换为JSON数组,可以使用Newtonsoft.Json.Linqnamespace中的JArray类。下面是一个例子:

using Newtonsoft.Json.Linq;

JObject obj = JObject.Parse(@"{
  'data': [
    {
      'id': 'testid',
      'variants': [
        {
          'name': 'testname',
          'description': 'testdesc',
          'images': {
            '2T': [
              {
                'id': 'testid',
                'product_id': 'testid1'
              }
            ]
          }
        }
      ]
    }
  ]
}");

JArray arr = new JArray(obj["data"][0]["variants"][0]["images"]["2T"]);

obj["data"][0]["variants"][0]["images"] = arr;

Console.WriteLine(obj.ToString());

这将输出:

{
  "data": [
    {
      "id": "testid",
      "variants": [
        {
          "name": "testname",
          "description": "testdesc",
          "images": [
            {
              "id": "testid",
              "product_id": "testid1"
            }
          ]
        }
      ]
    }
  ]
}
sd2nnvve

sd2nnvve2#

你的json字符串无效,应该是

{ "data": [
           {
            "id": "testid",
             ....
          }
        ]
}

你想要的JSON字符串也是无效的,但是你可以尝试用下面的代码来获得一个有效的带有图像数组的JSON

var jObj = JObject.Parse(json);

    var variants =jObj["data"].SelectMany(d => d["variants"]); 
    foreach (var variant in variants)
    {
        var newArr = new JArray();
        foreach (var prop in ((JObject)variant["images"]).Properties())
            newArr.Add(new JObject { ["Code"] = prop.Name, ["Data"] = prop.Value });
        variant["images"] = newArr;
    }

    json = jObj.ToString();

输出量

{
  "data": [
    {
      "id": "testid",
      "variants": [
        {
          "name": "testname",
          "description": "testdesc",
          "images": [
            {
              "Code": "2T",
              "Data": [
                {
                  "id": "testid",
                  "product_id": "productid1"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

相关问题