在 C# 中寻找正确的对象表示法来表示 JSON 样式的数据

Looking for correct object notation to represent JSON-style data in C#

我习惯于使用 JSON 制作 Web 应用程序,但现在我必须使用 C# 进行开发,而且我 运行 难以选择正确的对象表示法,因为我几乎没有接受过正规培训。

基本上我想在 C# 中重新创建以下 JSON:

{
    "10003": {
        "computer": 25,
        "desk": 23,
        "chair": 7,
        "score": 44
    },
    "10004": {
        "computer": 35,
        "desk": 23,
        "chair": 7,
        "score": 77
    },
    etc...
}

这样我就可以通过执行以下操作快速访问或更改任何值:

myObjectName["10003"]["score"] = 23

我还需要根据它们内部的 'score' 值对高级键(“10003”、“10004 等”)的顺序进行排序。

我一直在考虑键值对列表字典的某种组合,但在这种情况下我正在寻找 simplest/correct 表示法?

编辑:我不是要转换 JSON。我没有 JSON。我只是展示了我将如何使用 JSON 来做到这一点。我正在寻找原生 C# 方法,除非 C# 中的每个人都只使用 JSON?

您可以将其转换为数据集:

DataSet myDataSet= JsonConvert.DeserializeObject<DataSet>(jsonstring);

然后像这样更改数据:

myDataSet.Tables[0].Rows[0]["10003"]["score"]=23;

要考虑的一种方法是将其存储在 DictionaryDictionary 中,然后根据需要存储在 OrderBy 中:

static void Main(string[] args)
{
    var bob = new Dictionary<string, Dictionary<string, int>>();

    bob["10003"] = new Dictionary<string, int> {
        { "computer", 25 },
        { "desk", 23 },
        { "chair", 7 },
        { "score", 44} };
    bob["10004"] = new Dictionary<string, int> {
        { "computer", 35 },
        { "desk", 23 },
        { "chair", 7 },
        { "score", 77} };
    bob["10005"] = new Dictionary<string, int> {
        { "computer", 85 },
        { "desk", 23 },
        { "chair", 7 },
        { "score", 10} };

    var ordered = bob.OrderBy(z => z.Value["score"]).ToList();

    Console.WriteLine(ordered.First().Value["computer"]); // 85 since 10 is the lowest score
    Console.WriteLine(ordered.Last().Value["computer"]); // 35 since 77 is the highest score

    Console.ReadLine();
}