反序列化在C#JSON数组列表 [英] deserialize json array list in c#

查看:640
本文介绍了反序列化在C#JSON数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有作为后端主要是C#项目,但我不是一个有经验的C#开发,所以我不能够想出热修复对象的列表的JSON反序列化。下面的函数是采取反序列化的照顾,但我得到一个错误:

I'm working on a project which has as backend mainly C#, but I'm not an experienced C# dev so I'm not able to figure out hot to fix a json deserialization of an list of objects. The following function is what takes care of the deserialization, but I get an error :

using System.IO;
using System.Web;
using Raven.Imports.Newtonsoft.Json;

namespace Corina.Web.Handlers
{
    public class JsonRequestHandler
    {
        public T Handle<T>(HttpContextBase context)
        {
            string requestData;

            context.Request.InputStream.Position = 0;
            using (var inputStream = new StreamReader(context.Request.InputStream))
            {
                requestData = inputStream.ReadToEnd();
            }

            return JsonConvert.DeserializeObject<T>(requestData, new Raven.Imports.Newtonsoft.Json.Converters.StringEnumConverter());           
        }
    }
}

错误

无法反序列化当前的JSON数组(例如[1,2,3])入式
  Corina.Web.Views.DocumentViewModel,因为类型需要一个JSON
  对象(例如{名:值})。正确地反序列化

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Corina.Web.Views.DocumentViewModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

要解决这个错误要么改变的JSON到一个JSON对象(如
  {名:价值})或改变反序列化类型的数组或
  实现一个集合接口类型(如ICollection的,IList的)
  像列表可以从JSON数组反序列化。
  JsonArrayAttribute也可加入到用它强制类型
  从JSON反序列化数组

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

谁能告诉我,我该怎么做反序列化对象而不是一个对象的名单上吗?

Can anyone tell me how do I make the deserialization on a list of objects instead of an object ?

推荐答案

您必须创建这个类,并创建一个类似的方法如下:

You have to create this class and create a method like below :

   public class Demo
   {
      public string Name;
      public string Type;
      public string Value;
      public string ChildContentType;
      public string ChildMetadata;
   }

    public void Deserialize()
    {
        string jsonObjString = "[{\"Name\": \"Description\",\"Type\": \"Text\",\"Value\": \"XXX\",\"ChildContentType\": \"Value\",\"C??hildMetadata\": \"YYY\"}]";
         var ser = new JavaScriptSerializer();
         var arreyDemoObj = ser.Deserialize<Demo[]>(jsonObjString);

         foreach (Demo objDemo in arreyDemoObj)
         {
             //Do what you want with objDemo
         }
      }

请注意,你需要添加<参考href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\"相对=nofollow>的JavaScriptSerializer 。

这篇关于反序列化在C#JSON数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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