从 C# 中的 JSON 数组中只选择一条记录

Pick only one record from a JSON array in C#

我正在尝试从 JSON 数组中提取记录,但它似乎不起作用。 这是我的代码:

 DataTable table = ConvertJsonToDatatable(responseBody);
 System.Windows.Forms.MessageBox.Show(table.Columns[1].ToString(), "transformation");

MessageBox 未显示。我已经检查过 responseBody 并且变量根本不为空。 这里变量的结构(和JSON数组后面)

{
"data": 
    [
        [
          1651217520000,
          1.0562,
          1.0562,
          1.056,
          1.0561,
          0,
          0
        ],
        [
          1651217580000,
          1.0561,
          1.0563,
          1.0561,
          1.0561,
          0,
          0
        ]
    ],
          
"events": null
}

public static DataTable ConvertJsonToDatatable(string jsonString)
        {
            var jsonLinq = JObject.Parse(jsonString);
            // Find the first array using Linq
            var linqArray = jsonLinq.Descendants().Where(x => x is JArray).First();
            var jsonArray = new JArray();
            foreach (JObject row in linqArray.Children<JObject>())
            {
                var createRow = new JObject();
                foreach (JProperty column in row.Properties())
                {
                    // Only include JValue types
                    if (column.Value is JValue)
                    {
                        createRow.Add(column.Name, column.Value);
                    }
                }
                jsonArray.Add(createRow);
            }
            return JsonConvert.DeserializeObject<DataTable>(jsonArray.ToString());
        }

有没有人知道如何从这个数组(我的代码中是一个字符串)中 extract/pick 一个值?

祝大家周末愉快,提前致谢

您必须修复 table 创建代码

public static DataTable ConvertJsonToDatatable(string jsonString)
{
    var jsonLinq = JObject.Parse(jsonString);
    // Find the first array using Linq
    var linqArray = jsonLinq.Descendants().Where(x => x is JArray).First();

     //or maybe this would be enough
    var  linqArray = JObject.Parse(jsonString)["data"];

    var jsonArray = new JArray();
    foreach (var row in (JArray)linqArray)
    {
        var createdRow = new JObject();
        var i = 0;
        foreach (var item in row)
        {
            i++;
            createdRow.Add("col" + i.ToString(), (string)item);
        }
        jsonArray.Add(createdRow);
    }
    return jsonArray.ToObject<DataTable>();
}

如何使用

DataTable table = ConvertJsonToDatatable(responseBody);
string val = table.Rows[0].Field<string>("col2");
System.Windows.Forms.MessageBox.Show(val, "transformation");

我可能会被标记为负面,但我会尝试解释它的内部结构。我举了个例子来展示拖车以获得它可能为您可视化的数组列表。

void Main()
{
    string json = "{\"data\":[[1651217520000,1.0562,1.0562,1.056,1.0561,0,0],[1651217580000,1.0561,1.0563,1.0561,1.0561,0,0]],\"events\":null}";
    var obj = JObject.Parse(json);
    foreach (JToken token in obj.FindTokens("data"))
    {
        foreach (JArray row in JArray.Parse(token.ToString()))
        {
            row.Dump();
            row[0].Dump("first element");
            
        } 
    }
    
}

public static class JsonExtensions
{
    public static List<JToken> FindTokens(this JToken containerToken, string name)
    {
        List<JToken> matches = new List<JToken>();
        FindTokens(containerToken, name, matches);
        return matches;
    }

    private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
    {
        if (containerToken.Type == JTokenType.Object)
        {
            foreach (JProperty child in containerToken.Children<JProperty>())
            {
                if (child.Name == name)
                {
                    matches.Add(child.Value);
                }
                FindTokens(child.Value, name, matches);
            }
        }
        else if (containerToken.Type == JTokenType.Array)
        {
            foreach (JToken child in containerToken.Children())
            {
                FindTokens(child, name, matches);
            }
        }
    }
}


结果将是一个 jarray 数组

这样您就可以构建数据表行

.Dump() 是 linqpad 遗留下来的。好工具。与 console.write

相同