简体   繁体   English

从字典中提取数组值<string, object></string,>

[英]Extract array values from Dictionary<string, object>

I have this Json:我有这个 Json:

{
    "DataType": "Calibration_Symptoms",
    "playerHeight": 1.6739861965179443,
    "armSpan": 1.572389006614685,
    "calibratedHeadPosition": {
        "x": 0.10129322111606598,
        "y": 1.6739861965179443,
        "z": -0.01975761353969574
    },
    "symptomSeverity": 0,
    "scat5SymptomsQs": [1, 2, 3],
    "rightHanded": true,
    "rightFooted": true,
    "testType": false,
    "trialSymptomsQs": {
        "Gait": [1, 2, 3],
        "Balance1": []
    }
}

I deserialized it with var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseString);我用var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseString);反序列化了它. .

I'm now trying to extract the array values in scat5SymptomsQs .我现在正在尝试提取scat5SymptomsQs中的数组值。 How do I do that?我怎么做?

Dictionary<string,object> is a wrong type. Dictionary<string,object>是错误的类型。 Create a class that represents the object.创建一个代表 object 的 class。

class Data
{
    public List<int> scat5SymptomsQs { get; set; }
}

Then you can:那么你也能:

var o = JsonConvert.DeserializeObject<Data>(json);

If you don't want to create the object then please see LINQ to JSON .如果您不想创建 object,请参阅LINQ 至 JSON

using System.Linq;
using Newtonsoft.Json.Linq;

(...)

JObject o = JObject.Parse(json);

IList<int> x = o["scat5SymptomsQs"].Select(t => (int)t).ToList();

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

相关问题 获取 object 值字典数组的 object 值“字典<string, object[]> ”</string,> - Get values of an object of dictionary array of object value “ Dictionary<string, object[]>” 转换字典中的值 <string, object> 与T - Convert values from a Dictionary<string, object> with T C#-从字符串类型为object的Dictionary中的对象获取数组值 - C# - Get array values from object in Dictionary of type string, object 从C#中的对象中提取字符串数组 - Extract a string array from an object in c# 如何使用c#从字符串数组/字典中提取数字? - How to extract numbers from string array/dictionary using c#? 无法从对象数组中提取值,javascript 数组已反序列化到该数组中 - Unable to extract values from object array, to which javascript array was deserialized 如何从字典中收集所有键的值 <string,object> 在清单中 - How to collect values for all the keys from a Dictionary<string,object> in a List C#从字典获取值 <string, List<object> &gt; - C# getting values from Dictionary<string, List<object>> 从IEnumerable <Dictionary <string,object >>获取键和值 - Getting Keys and Values from IEnumerable<Dictionary<string, object>> 如何从C#中的字典和对象中获取所有字符串值 - how to get all of string values from dictionary and object in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM