繁体   English   中英

如何反序列化包含可变数量对象的json对象并将其作为C#中的键值集合?

[英]How to de-serialize a json object containing variable number of objects and get them as a key value collection in C#?

我如何反序列化以下JSON对象并获取Dictionary的集合,其中key(string)应为方法名称,而对象为C#中的详细信息。

{
"methods": {
    "password.2": {
      "title": "Password CustomerID",
      "type": "password"
    },
    "ubikey.sms.1": {
      "title": "SMS",
      "type": "stepup",
      "password": "password.2",
      "stepUp": "sms"
    },
    "tupas.test.1": {
      "title": "TUPAS Emulator",
      "type": "proxy"
    }
  }
}

如果它是一个方法数组,我可以很容易地使用数组序列化它。 但是由于那本身就是一个键值对,所以我很困惑。

我正在向特定的api发出WebRequest,并将结果作为json获取。

用于序列化对象的代码来自https://msdn.microsoft.com/zh-cn/library/hh674188.aspx

JSON.net这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            string json = @"{
""methods"": {
    ""password.2"": {
      ""title"": ""Password CustomerID"",
      ""type"": ""password""
    },
    ""ubikey.sms.1"": {
      ""title"": ""SMS"",
      ""type"": ""stepup"",
      ""password"": ""password.2"",
      ""stepUp"": ""sms""
    },
    ""tupas.test.1"": {
      ""title"": ""TUPAS Emulator"",
      ""type"": ""proxy""
    }
  }
}";

            Dictionary<string, Dictionary<String,Dictionary<String,String>>> values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<String,Dictionary<String,String>>>>(json);
        }


    }
}

链接到的页面描述了如何使用DataContractJsonSerializer反序列化JSON。 您可以使用该序列化器将"methods"对象反序列化为Dictionary前提是您使用的是.Net 4.5或更高版本 ,还可以设置DataContractJsonSerializerSettings.UseSimpleDictionaryFormat = true 完成此操作后,您可以定义以下类:

public class Method
{
    public string title { get; set; }
    public string type { get; set; }
    public string password { get; set; }
    public string stepUp { get; set; }
}

public class Response
{
    public Dictionary<string, Method> methods { get; set; }
}

并将它们解析如下:

        var settings = new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true };
        var jsonSerializer = new DataContractJsonSerializer(typeof(Response), settings);
        var jsonResponse = jsonSerializer.ReadObject(response.GetResponseStream()) as Response;

如果使用的是.Net的早期版本,则可能需要使用其他序列化器(例如Json.NET) ,因为在早期版本中, DataContractJsonSerializer将字典序列化为键/值对数组

 dynamic results = JsonConvert.DeserializeObject(JsonString());
 Dictionary<string, object> dictionary = new Dictionary<string, object>();
 foreach (var o in results.methods)
 {
     dictionary[o.Name] = o.Value;
 }

private static string JsonString()
{
         return "{\"methods\": {\"password.2\": {\"title\": \"Password CustomerID\",\"type\": \"password\"},\"ubikey.sms.1\": {\"title\": \"SMS\",\"type\": \"stepup\",\"password\": \"password.2\",\"stepUp\": \"sms\"},\"tupas.test.1\": {\"title\": \"TUPAS Emulator\",\"type\": \"proxy\"}}}";
}

暂无
暂无

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

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