2015-11-03 35 views
0

我正在接收来自外部源的JSON数据,我可能会收到多个不同的东西,我需要能够在运行时解析它们以确定我正在接收什么(取而代之简单地反序列化为预先构建的对象)。我该如何才能JavaScriptSerializer反序列化在运行时定义的JSON

我已经能够反序列化到字符串的解释:

var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(json) 

然后我就可以做逻辑,如:

if(dict.ContainsKey("MyItem")) 
{ 
    doA(); 
} 
if(dict.ContainsKey("UnknownItem")) 
{ 
    setError(); 
} 

然而,这只会如果所有的项目工作JSON对象是字符串。我如何得到这个使用任意类型?

回答

0

诀窍是将字典用于反序列化的类型。这将导致反序列化器构建正确的本机类型,然后可以根据需要将它们转换为您的类型。

  • JSON字符串 - > C#字符串
  • JSON数 - > C#INT(如果没有小数点)
  • JSON数 - > C#十进制(如果小数点)
  • JSON布尔 - > C#布尔
  • JSON列表 - > C#的ArrayList
  • JSON字典 - > C#字典

这里有一个例子是画家trates所有类型转换:

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Script.Serialization; 

namespace JsonDeserialize 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     string test = 
      "{" + 
      " \"string_type\":\"test_string\",\n" + 
      " \"int_type\":42,\n" + 
      " \"float_type\":3.141592654,\n" + 
      " \"bool_type\":true,\n" + 
      " \"list_type\":[\n" + 
      "  \"list_one\",\n" + 
      "  \"list_two\"\n" + 
      " ],\n" + 
      " \"dict_type\":{\n" + 
      "  \"dict_one\":\"dict_one\",\n" + 
      "  \"dict_two\":\"dict_two\"\n" + 
      " }\n" + 
      "}\n"; 


     var dict = new JavaScriptSerializer().Deserialize< 
      Dictionary<string, object>>(test); 

     string result_string = ""; 
     int result_int = 0; 
     decimal result_float = 0.0M; 
     bool result_bool = false; 
     ArrayList result_list = new ArrayList(); 
     string result_list_one = ""; 
     string result_list_two = ""; 
     Dictionary<string, object> result_dict = new Dictionary<string,object>(); 
     string result_dict_one = ""; 
     string result_dict_two = ""; 

     if (dict.ContainsKey("string_type")) 
     { 
      result_string = (String)dict["string_type"]; 
     } 
     if (dict.ContainsKey("int_type")) 
     { 
      result_int = (int)dict["int_type"]; 
     } 
     if (dict.ContainsKey("float_type")) 
     { 
      result_float = (decimal)dict["float_type"]; 
     } 
     if (dict.ContainsKey("bool_type")) 
     { 
      result_bool = (bool)dict["bool_type"]; 
     } 
     if (dict.ContainsKey("list_type")) 
     { 
      result_list = (ArrayList)dict["list_type"]; 
      result_list_one = (String)result_list[0]; 
      result_list_two = (String)result_list[1]; 
     } 
     if (dict.ContainsKey("dict_type")) 
     { 
      result_dict = (Dictionary<string,object>)dict["dict_type"]; 
      result_dict_one = (String)result_dict["dict_one"]; 
      result_dict_two = (String)result_dict["dict_two"]; 
     } 


     ////////////////// TEST ////////////////// 
     // Below here is just a check of the data coming out 
     string expected_string = "test_string"; 
     int expected_int = 42; 
     decimal expected_float = 3.141592654M; 
     bool expected_bool = true; 
     string expected_list_one = "list_one"; 
     string expected_list_two = "list_two"; 
     string expected_dict_one = "dict_one"; 
     string expected_dict_two = "dict_two"; 

     Console.WriteLine("Results..."); 
     if (result_string == expected_string) 
     { 
      Console.WriteLine("String Passed"); 
     } 
     else 
     { 
      Console.WriteLine("String Failed"); 
      Console.WriteLine("Result: " + result_string); 
     } 

     if (result_int == expected_int) 
     { 
      Console.WriteLine("Int Passed"); 
     } 
     else 
     { 
      Console.WriteLine("Int Failed"); 
      Console.WriteLine("Result: " + result_int.ToString()); 
     } 

     if (result_float == expected_float) 
     { 
      Console.WriteLine("Float Passed"); 
     } 
     else 
     { 
      Console.WriteLine("Float Failed"); 
      Console.WriteLine("Result: " + result_float); 
     } 

     if (result_bool == expected_bool) 
     { 
      Console.WriteLine("Bool Passed"); 
     } 
     else 
     { 
      Console.WriteLine("Bool Failed"); 
      Console.WriteLine("Result: " + result_bool); 
     } 

     if ((result_list_one == expected_list_one) && 
      (result_list_two == expected_list_two)) 
     { 
      Console.WriteLine("List Passed"); 
     } 
     else 
     { 
      Console.WriteLine("List Failed"); 
      Console.WriteLine("Result: " + result_list_one + " " + result_list_two); 
     } 

     if ((result_dict_one == expected_dict_one) && 
      (result_dict_two == expected_dict_two)) 
     { 
      Console.WriteLine("Dict Passed"); 
     } 
     else 
     { 
      Console.WriteLine("Dict Failed"); 
      Console.WriteLine("Result: " + result_dict_one + " " + result_dict_two); 
     } 

     Console.WriteLine("Test Complete"); 
     } 
    } 
} 
相关问题