如何从json对象获取密钥并转换为数组? [英] how to get the key from json object and convert into an array?

查看:119
本文介绍了如何从json对象获取密钥并转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
 "Date": "2016-12-15",
 "Data": {
   "A": 4.4023,
   "AB": 1.6403,
   "ABC": 2.3457
 }
}

我如何将我的钥匙A,Ab,ABC放入一个阵列中?只是键而不是值.

how can i get my keys A,Ab,ABC into an array? just the keys not value.

推荐答案

您可以安装来查询属性:

You could install json.net and use LINQ to JSON to query the properties:

var jsonString = @"{
     ""Date"": ""2016-12-15"",
     ""Data"": {
       ""A"": 4.4023,
       ""AB"": 1.6403,
       ""ABC"": 2.3457
     }
}";

var root = JToken.Parse(jsonString);

var properties = root
    // Select nested Data object
    .SelectTokens("Data")
    // Iterate through its children, return property names.
    .SelectMany(t => t.Children().OfType<JProperty>().Select(p => p.Name))
    .ToArray();

Console.WriteLine(String.Join(",", properties)); // Prints A,AB,ABC

示例小提琴.

这篇关于如何从json对象获取密钥并转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆