C#需要反序列化动态JSON以绑定到ListView

C# Need to deserialize dynamic JSON to bind to ListView
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我有以下格式的JSON:

{
    "fields": 
    [
        "field_a",
        "field_b"
    ],
    "records": 
    [
        [
            1203,
            169
        ],
        [
            983,
            147
        ]
    ]
}

我需要反序列化并将其绑定到ASP.NET ListView,其中"字段"成为列标题,而"记录"成为数据行.

在这种情况下,我将有一个表:

field_a | field_b
-----------------
1203    | 169
983     | 147

我目前正在使用Newtonsoft JSON库进行反序列化,但无法弄清楚如何将动态数量的字段映射到最终可绑定到Web控件的C#对象中.

解决过程1

我认为您在问题中几乎使用了关键字.动态宝贝.这是一个入门的示例.

var text = "{\"fields\": [\"field_a\",\"field_b\"],\"records\": [[1203,169],[983,147]]}";
dynamic result = JsonConvert.DeserializeObject(text);
var fields = result.fields;
var records = result.records;
var columnList = new List<Column>();

for (var x = 0; x < fields.Count; x++)
{
    var field = fields[x];
    var col = new Column() {Title = field.Value};
    columnList.Add(col);
    var record = records[x];
    for (var y = 0; y < record.Count; y++)
    {
        var value = record[y];
        var cell = new Cell() {Value = (int)value.Value};
        col.Cells.Add(cell);
    }
}

和要匹配的类:

public class Column
{
    public Column()
    {
        Cells = new List<Cell>();
    }
    public string Title { get; set; }
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    public int Value { get; set; }
}

这只是一个例子.我不确定您希望自己的班级看起来如何,但这只是一个开始.希望这会有所帮助.

I have JSON in the following format:

{
    "fields": 
    [
        "field_a",
        "field_b"
    ],
    "records": 
    [
        [
            1203,
            169
        ],
        [
            983,
            147
        ]
    ]
}

which I need to de-serialize and bind to an ASP.NET ListView, where 'fields' becoming the column headings, and 'records' becoming the rows of data.

In this case, I would have a table:

field_a | field_b
-----------------
1203    | 169
983     | 147

I am currently using Newtonsoft JSON library to de-serialize, but can't figure out how to map a dynamic number of fields into a C# object that is ultimately bind-able to the web control.

Solutions1

I think you pretty much used the keyword in your question. Dynamic baby. here is an example to get you started.

var text = "{\"fields\": [\"field_a\",\"field_b\"],\"records\": [[1203,169],[983,147]]}";
dynamic result = JsonConvert.DeserializeObject(text);
var fields = result.fields;
var records = result.records;
var columnList = new List<Column>();

for (var x = 0; x < fields.Count; x++)
{
    var field = fields[x];
    var col = new Column() {Title = field.Value};
    columnList.Add(col);
    var record = records[x];
    for (var y = 0; y < record.Count; y++)
    {
        var value = record[y];
        var cell = new Cell() {Value = (int)value.Value};
        col.Cells.Add(cell);
    }
}

and the classes to match:

public class Column
{
    public Column()
    {
        Cells = new List<Cell>();
    }
    public string Title { get; set; }
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    public int Value { get; set; }
}

This is just an example. I am not sure how you want your classes to look but this would be a start. Hopefully this helps.

转载于:https://stackoverflow.com/questions/23003728/c-sharp-need-to-deserialize-dynamic-json-to-bind-to-listview

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入