2

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());           
        }
    }
}

Error :

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.

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 ?

9
  • For me what I understand is that you send together 2 JSON objects (like you have append the one to the other) and can not deserialize them together and ask you ether send one object, ether convert it to List<> Commented Feb 27, 2013 at 9:19
  • Can't you use JavaScriptSerializer? Commented Feb 27, 2013 at 9:22
  • Well, yeah, I send something like : [ Object, Object, .... ] ; so that cannot be converted. Commented Feb 27, 2013 at 9:22
  • I would use it I knew how :) Commented Feb 27, 2013 at 9:24
  • @Roland: Can you give me the structure of your object? Commented Feb 27, 2013 at 9:54

2 Answers 2

4

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
         }
      }

Note that you need to add reference for JavaScriptSerializer.

Sign up to request clarification or add additional context in comments.

Comments

0

Don't know the structure of your json data but i guess you are using some custom classes to deserialize with DataContractJsonSerializer you can deserialize in the following manner

Json list:

var testdata = "[{\"name\":\"numToRetrieve\",\"value\":\"3\",\"label\":\"Number of     items       to retrieve:\"},{\"name\":\"showFoo\",\"value\":\"on\",\"label\":\"Show foo?\"},  {\"name\":\"title\",\"value\":\"Foo\",\"label\":\"Foo:\"}]";

DataContractJsonSerializer js = 
new DataContractJsonSerializer(typeof (List<FooDef>));
var stream = new MemoryStream(Encoding.UTF8.GetBytes(testdata));
var foo = js.ReadObject(stream);
stream.Close();



[DataContract]
public sealed class FooDef
{
    [DataMember(Name = "name", IsRequired = true)]
    public string Name { get; set; }

    [DataMember(Name = "value", IsRequired = true)]
    public string Value { get; set; }

    [DataMember(Name = "label", IsRequired = true)]
    public string Label { get; set; }
} 

1 Comment

using System.Runtime.Serialization.Json; required for DataContractJsonSerializer. Compared to using System.Web.Script.Serialization; required for JavaScriptSerializer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.