简体   繁体   English

从C#返回带有数字键的列表

[英]Return list with numeric keys from C#

My code is as follows: 我的代码如下:

var aaData = 
    ctx.PaymentRates
        .Where(x => x.ServiceRateCodeId == new Guid("BBCE42CB-56E3-4848-B396-4656CCE3CE96"))
        .Select(x => new 
        {
            Id = x.Id
        }).ToList();

It generates the following JSON when converted using Json(aaData); 使用Json(aaData);转换时,它将生成以下JSON Json(aaData); :

"aaData":[
    {"Id":"ab57fc9d-ffb7-4a12-8c5c-03f36b4ef1fe"},
    {"Id":"4c1e9776-5d64-4054-a9c9-0fc8b8b8e8a1"}
    etc.
]

However, I would like to return keys before the values, like so: 但是,我想在值之前返回键,如下所示:

"aaData":[
    [0] => {"Id":"ab57fc9d-ffb7-4a12-8c5c-03f36b4ef1fe"},
    [1] => {"Id":"4c1e9776-5d64-4054-a9c9-0fc8b8b8e8a1"}
    etc.
]

Edit: I am not sure of the proper syntax - the point is, I just want numeric keys. 编辑:我不确定正确的语法-问题是,我只想要数字键。

How can I do this in C#? 如何在C#中做到这一点?

JSON uses implicit indexing - meaning the order of elements in an array is preserved, so you can infer the "index" from the order of elements when you retrieve them. JSON使用隐式索引-意味着保留数组中元素的顺序,因此您可以在检索元素时从元素的顺序推断 “索引”。

If you need to include an "index" element, you'll have to make it an attribute of the array element: 如果需要包括“索引”元素,则必须使其成为数组元素的属性:

var aaData = 
    ctx.PaymentRates
        .Where(x => x.ServiceRateCodeId == new Guid("BBCE42CB-56E3-4848-B396-4656CCE3CE96"))
        .Select((x, i) => new 
        {
            Index = i,
            Id = x.Id
        }).ToList();

Your JSON should look something like: 您的JSON应该类似于:

"aaData":[
    {"Index":"0", "Id":"ab57fc9d-ffb7-4a12-8c5c-03f36b4ef1fe"},
    {"Index":"1", "Id":"4c1e9776-5d64-4054-a9c9-0fc8b8b8e8a1"}
    etc.
]

It is not a matter of whether it is possible in C# or not, it is more a matter of the fact that JSON has a certain format and the format you would like to achieve is not compliant with JSON. 这与是否可以在C#中实现无关紧要,更多的是,JSON具有某种格式,而您想要实现的格式与JSON不兼容。

So in case you would like to achieve a result in such a format you would have to build your own format (but then you would also have to create your own serializers, deserializers, etc - in general quite a lot of hassle). 因此,如果您希望以这种格式获得结果,则必须构建自己的格式(但随后还必须创建自己的序列化器,反序列化器等,通常会产生很多麻烦)。

Do you mean the index? 你是指指数吗? Here's one way you can get it 这是获得它的一种方法

var counter = 0;
var aaData = 
    ctx.PaymentRates
        .Where(x => x.ServiceRateCodeId == new Guid("BBCE42CB-56E3-4848-B396-4656CCE3CE96"))
        .Select(x => new 
        {
            Index = counter++,
            Id = x.Id
        }).ToList();

In order to do what you wish you would have to use a Dictionary instead of a List. 为了执行您希望的操作,您将必须使用字典而不是列表。 Doing: 正在做:

int idx = 0;
var dict = aaData.ToDictionary(k => "" + idx++);

will return provide you with the following valid JSON 将返回为您提供以下有效的JSON

"aaData":{
    "0" : {"Id":"ab57fc9d-ffb7-4a12-8c5c-03f36b4ef1fe"},
    "1" : {"Id":"4c1e9776-5d64-4054-a9c9-0fc8b8b8e8a1"}
    etc.
}

note the fact that the keys are strings that contain numeric values. 请注意,键是包含数字值的字符串。 Numeric values are not supported as keys . 不支持将数值用作键

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

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