如何将对象的json数组反序列化为字典 [英] How to deserialize the json array of objects to dictionary

查看:185
本文介绍了如何将对象的json数组反序列化为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Json字符串

I have a Json string

"[{'SymptID': '" + MidID + "', 'ALvl' : '" +JawLvl + "'},
{'SymptID': '" + BckID + "', 'ALvl' : '" + PanLvl + "'},
{'SymptID': '" + SysID + "', 'ALvl' : '" + wPLvl + "'}]"

 

Dictionary<string, string> Symptoms =
    (Dictionary<string, string>)Newtonsoft.Json.JsonConvert.DeserializeObject(
        data, typeof(Dictionary<string, string>));

foreach (KeyValuePair<string, string> keyValuePair in Symptoms)
{ //do some action }

如何使它能够使用此多个数组json数据获取键和值...是否还有其他方法.

how can make this get keys and values with this multiple array json data... is there any other methods.

注意:问题不是重复 到如何反序列化JSON到一个简单的Dictionary< string,string>在ASP.NET中?, 因为json的结构不同.

NOTE:The question is not a duplicate to How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?, because json structure is different.

这里我们有一个对象数组,具有2个属性SymptID和ALvl, 需要使用键,SymptID 值ALvl..被引问题有一个具有多个属性的对象,这些属性的对应值需要转换为 以属性名称为键,以属性值作为值的字典 字典.

Here we have an array of objects with 2 properties SymptID and ALvl, that need to be converted to dictionary with key SymptID and value ALvl.  Referred question has one object with multiple properties having corresponding values that needs to be converted to dictionary with property name as key  and property value as value in dictionary.

推荐答案

与内置的.net序列化程序相比,您还可以使用json.net或fastjson开源库以更快,更高效的方式执行此任务.

you can also use json.net or fastjson opensource libraries to perform this task in a faster and efficient way than using the built in .net serializer.

json.net- http://json.codeplex.com/

json.net - http://json.codeplex.com/

fastJson- http://www.codeproject.com/Articles/159450/fastJSON

fastJson - http://www.codeproject.com/Articles/159450/fastJSON

这篇关于如何将对象的json数组反序列化为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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