简体   繁体   中英

Extract array values from Dictionary<string, object>

I have this 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);.

I'm now trying to extract the array values in scat5SymptomsQs . How do I do that?

Dictionary<string,object> is a wrong type. Create a class that represents the object.

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 .

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

(...)

JObject o = JObject.Parse(json);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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