简体   繁体   English

如何将字符串转换为字典列表

[英]How to convert string to list of dictionary

I have a string from http response我有一个来自 http 响应的字符串

data = "[{'value':123,'Type':'Range'},{'value':456,'Type':'Fixed'}]"

how can i convert it into list of dict in c#我如何将其转换为 C# 中的 dict 列表

There should be no issue there.那里应该没有问题。

With either deserialising it to List<customObject> or List<Dictionary<string,string>>将其反序列化为List<customObject>List<Dictionary<string,string>>
using Json.net library.使用Json.net库。

public class Data{
    public string value{get;set;}
    public string Type{get;set;}
}

var testClass = JsonConvert.DeserializeObject<List<Data>>(input);   

Object dump:对象转储:

Dumping object(System.Collections.Generic.List`1[Data])  
[  
   {  
       Type   : Range  
       value  : 123  
   },  
   {  
       Type   : Fixed  
       value  : 456  
   }  
]

Or directly :或直接:

var testDict = JsonConvert.DeserializeObject<List<Dictionary<string,string>>>(input);   

Result :结果 :

Dumping object(  
  System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[String,String]])  
[  
   {  
    [  
           [value, 123]  
           ,  
           [Type, Range]  
    ]   },  
       {  
    [  
           [value, 456]  
           ,  
           [Type, Fixed]  
    ]     
   }  
]  

Don't forget the using Newtonsoft.Json;不要忘记using Newtonsoft.Json;

LiveDemo现场演示

The better way is to use simple list of object : First create class like below更好的方法是使用简单的对象列表:首先创建如下类

     public class respObject
     {
       public int Value { get; set; }
       public string  Type { get; set; }
     }

then DeserializeObject using Newtonsoft.Json as然后 DeserializeObject 使用 Newtonsoft.Json 作为

        var data = "[{'value':123,'Type':'Range'},{'value':456,'Type':'Fixed'}]";

        var objList = JsonConvert.DeserializeObject<List<respObject>>(data);

But answer to your Question list of dict但是回答你的 dict 问题列表

         var data = "[{'value':123,'Type':'Range'},{'value':456,'Type':'Fixed'}]";

         var listDict = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(data);

You can try with JArray你可以试试JArray

var data = "[{'value':123,'Type':'Range'},{'value':456,'Type':'Fixed'}]";
var dict = JArray.Parse(data)
                 .ToDictionary(k => k["value"].ToString(), v => v["Type"].ToString());

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

相关问题 如何转换List <String> 到字典 <int,String> - How to convert List<String> to Dictionary<int,String> 将字符串列表转换为字典 - Convert String list to a dictionary 如何转换字典 <string, object> 列出 <T> - How to convert Dictionary<string, object> to List<T> 如何转换列表<Claim>到字典<string,object> ? - How to convert List<Claim> to Dictionary<string,object>? 如何转换`字典 <string, Dictionary<string, List<MyCustomClass> &gt;&gt;`至`Dictionary <string, List<MyCustomClass> &gt;` - How to convert `Dictionary<string, Dictionary<string, List<MyCustomClass>>>` to `Dictionary<string, List<MyCustomClass>> ` 如何转换清单 <Class> 到字典 <string, List<String> &gt;在C#中? - How to convert List<Class> to Dictionary<string, List<String>> in C#? 转换清单 <Dictionary<string, string> &gt;到字典 <string, Dictionary<string, string> &gt; - convert List<Dictionary<string, string>> to Dictionary<string, Dictionary<string, string>> 如何转换List <string> 到字典 <string,string> 使用linq? - How to convert List<string> to Dictionary<string,string> using linq? 如何在c#中将字符串列表列表转换为字典 - how to convert list of list of string to dictionary in c# 如何将SpecFlow表转换为字典 <string, List<string> &gt; C# - How to convert a SpecFlow table into a Dictionary<string, List<string>> c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM