杰森反序列化到一类 [英] Json Deserialization to a class

查看:116
本文介绍了杰森反序列化到一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有动态的json结果,我想为该json字符串创建一个对象.之后,我将用反序列化的对象填充该对象.这是json字符串:

I have dynamic json result and i want to create an object for that json string. After that i will fill that object with the deserialized object. Here is the json string:

[{"_34":{
   "Id":"34",
   "Z":["42b23718-bbb8-416e-9241-538ff54c28c9","c25ef97a-89a5-4ed7-89c7-9c6a17c2413b"],
   "C":[]
   }
}]

对象的外观如何?或者如何将这个字符串反序列化为一个类.

How does the object look like? Or how can i deserialize this string to a class.

谢谢.

推荐答案

您可以使用 json.net 如果您喜欢开源的东西.

You can use the JavaScriptSerializer which available out of the box or json.net if you prefer something open source.

基于 Darin Dimitrov的示例,这是处理json.net的方法:

Based on Darin Dimitrov's sample, here's how you'd do with json.net:

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

namespace ConsoleApplication1
{
   class Program
   {
          static void Main(string[] args)
          {
              string json = "[{\"_34\":{ \"Id\":\"34\", \"Z\":[\"42b23718-bbb8-416e-9241-538ff54c28c9\",\"c25ef97a-89a5-4ed7-89c7-9c6a17c2413b\"], \"C\":[] } }]";
              var result = JsonConvert.DeserializeObject<Dictionary<string, Result>[]>(json);
              Console.WriteLine(result[0]["_34"].Z[1]);
           }
   }

   public class Result
   {
        public string Id { get; set; }
        public string[] Z { get; set; }
        public string[] C { get; set; }
   }
}

这篇关于杰森反序列化到一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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