【问题标题】:Removing Outer Bracket and Naming object with number?删除外括号并用数字命名对象?
【发布时间】:2020-04-13 21:41:10
【问题描述】:

我对 json 感到非常困惑(我还在学习 json)。

我想要的 JSON:

{  "entityMap": {
    "0": {
      "type": "LINK",
      "mutability": "MUTABLE",
      "data": {
        "url": "https://stackoverflow.com/"
      }
    },
    "1": {
      "type": "LINK",
      "mutability": "MUTABLE",
      "data": {
        "url": "https://stackoverflow.com/"
      }
    },
    "2": {
      "type": "LINK",
      "mutability": "MUTABLE",
      "data": {
        "url": "https://stackoverflow.com/"
      }
    },
    "3": {
      "type": "LINK",
      "mutability": "MUTABLE",
      "data": {
        "url": "https://stackoverflow.com/"
      }
    }
  }
}

这被认为是错误的 json 吗?以及如何用这样的数字命名json?

到目前为止,这是我得到的。

{
    "entityMap": [
        {
            "type": "LINK",
            "mutability": "MUTABLE",
            "data": {
                "url": "https://stackoverflow.com/"
            }
        },
        {
            "type": "LINK",
            "mutability": "MUTABLE",
            "data": {
                "url": "https://stackoverflow.com/"
            }
        },
        {
            "type": "LINK",
            "mutability": "MUTABLE",
            "data": {
                "url": "https://stackoverflow.com/"
            }
        }
    ]
}

这是我的课

    public class editorRawTest
    {
        public List<entityMapItem> entityMap { get; set; }

    }


    public class entityMapItem
    {
        public string type { get; set; }
        public string mutability { get; set; }
        public entityMapItemData data { get; set; }
    }

    public class entityMapItemData
    {
        public string url { get; set; }

    }

我的执行代码:

                var map = new List<entityMapItem>();

                var mapitem = new entityMapItem() { type = "LINK", mutability = "MUTABLE", data = new entityMapItemData() { url = "https://stackoverflow.com/" }  };

                map.Add(mapitem);
                map.Add(mapitem);
                map.Add(mapitem);



                editorRawTest bc = new editorRawTest() { entityMap = map };

                string JSONresult = JsonConvert.SerializeObject(bc);
                string path = @"jsonmapdata.json";
                using (var tw = new StreamWriter(path, true))
                {
                    tw.WriteLine(JSONresult.ToString());
                    tw.Close();
                }

通过谷歌和stackoverflow搜索没有运气。 任何线索或帮助将不胜感激。

谢谢。

【问题讨论】:

  • 我认为,第一个 json 不好,为什么你需要添加任何对象的数量。你可以像第二个json那样使用数组吗?
  • 真正的问题是什么? The JSON I want to &lt;JSON&gt; 也无济于事。

标签: c# .net json serialization json.net


【解决方案1】:

要获取第一个 JSON,您需要将 List&lt;entityMapItem&gt; 替换为 Dictionary&lt;string, entityMapItem&gt;,如下所示:

public class editorRawTest
{
    public Dictionary<string, entityMapItem> entityMap { get; set; }
}

那么你需要这样填写:

var map = new Dictionary<string, entityMapItem>();

var mapitem = new entityMapItem() { type = "LINK", mutability = "MUTABLE", data = new entityMapItemData() { url = "https://stackoverflow.com/" }  };

for (int i = 0; i < 4; i++)
{
    map.Add(i.ToString(), mapitem);
}

但我不鼓励你这样做,除非你必须这样做。您现在拥有的(列表中的第二个 JSON)更容易使用。如果您在两者之间进行选择,最好选择第二种方法。请参阅Using json key to store value, is it a good approach? 了解原因。

【讨论】:

  • 我不知道先生,但它的代码来自前程序员。在我个人看来,我喜欢第二个,但“其他人”可能不喜欢它。所以我别无选择,只能坚持第一个。
  • 没关系;如果你别无选择,那就没有什么好争论的了。 (根据您的问题,我不确定您是在设计新的东西还是只是尝试使用现有的 JSON。)
  • 我使用现有的 json。对不起这个问题,我的英语能力有限,所以我很困惑如何说出我的想法。但是感谢您的帮助,我真的很感激,通常我使用逐字字符串,但现在我尝试使用 json.net(让我保持理智)
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 1970-01-01
  • 2021-04-11
  • 2020-02-01
  • 1970-01-01
  • 2014-08-29
相关资源
最近更新 更多