2016-02-18 53 views
0

我有一個包含字典以及列表的JSON字符串對於解析,存在一個將列表類型從JSON字符串轉換爲字典的函數。我需要編寫一個將Dictionary類型轉換爲List類型的函數。什麼將是最有效的方式來做到這一點?將複雜Json中的字典轉換爲列表<string>

例如...

想這是我的示例JSON字符串 -

"{\"foo1\":{\"0\":\"0\",\"1\":\"S\",\"2\":\"S\",\"3\":\"J\",\"4\":\"Q\",\"5\":\"X\",\"6\":\"M\"},\"foo2\":{\"1\":\"one\" ,\"2\":\"two\",\"4\":\"four\",\"5\":\"five\",\"6\":\"six\",\"7\":\"seven\",\"8\":\"eight\"}" 

這是一種有效的方式從這裏走字典中的值,並將其轉換爲字符串列表。

+1

http://stackoverflow.com/questions/11470305/convert-dictionarystring-int-into-listobject – krichalka

回答

1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication11 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      string JSONIncludeBackslash = "{\"foo1\":{\"0\":\"0\",\"2\":\"S\",\"3\":\"J\",\"4\":\"Q\",\"5\":\"X\",\"6\":\"M\"},\"foo2\":{\"1\":\"one\",\"7\":\"seven\",\"8\":\"eight\"}}"; 
      Dictionary<string, string> JSONDictionary = JSONIncludeBackslash.Replace("\"", "").Replace(":{", "*").Replace("},", ",").Replace("}}", "").Replace("{", "").Replace("}", "").Split(',').ToDictionary(value => { return value.Split(':')[0].IndexOf("*") > -1 ? value.Split(':')[0].Split('*')[1] : value.Split(':')[0]; }); 

      Dictionary<string, string> JSONDictionary1 = JSONIncludeBackslash.Replace("\"", "").Replace(":{", "*").Replace("},", ",").Replace("}}", "").Replace("{", "").Replace("}", "").Split(',').ToDictionary(value => { return value.Split(':')[0].IndexOf("*") > -1 ? value.Split(':')[0].Split('*')[1] : value.Split(':')[0]; }); 
      foreach (var Entry in JSONDictionary1) 
      { 
       JSONDictionary[Entry.Key] = Entry.Value.Split(':')[1]; 
      } 

      IList<KeyValuePair<string, string>> JSONList = JSONDictionary.ToList(); 
      foreach (var Item in JSONList) 
      { 
       Console.WriteLine(Item); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

enter image description here