2

I'm really new too Json and I'm trying to call a Web Service.

When the service return an array of one element de json string for this array is without the [ ]. This cause a Exception in the serializer I use. (I use this one http://james.newtonking.com/)

My question is simple can I add something too tell to the deserializer to always take this section for a Array

In my code i have this model class

public class Company : BaseEntity
{
    #region Constructors
    public Company()
    {

    }

    public Company(int id, string name, string description)
    {
        Id = id;
        Name = name;
        Description = description;
    }
    #endregion

    #region Properties
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<Industry> IndustryList { get; set;}
    #endregion

}

And the Json for a company with one industry is

{"company":{"description":"Societe de Google","id":"0","industryList":{"id":"0","name":"Technologies Cool"},"name":"Google Inc."}

Maybe, i should change the serilizer tool too, I'm open too. All work find with other list of 2 or more elements or if I change the List for Industry but sometime I will receive more than one industry.

Thank you.

3
  • This behavior is normal for JSON services (and I hate it). I would say you could consider it a bug in the deserializer you use - perhaps contact the creator of the deserializer? Maybe it is fixed in the current version. Commented Oct 16, 2010 at 7:29
  • This is the exception I get. Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[StockOverflow.Client.Logic.Models.Entities.Industry]'. Commented Oct 16, 2010 at 19:50
  • Have you found an answer to this, @Yannick ? I have the same problem with an API ... Commented Jan 27, 2012 at 20:38

1 Answer 1

3

Make your class

[DataContract]
public class Company : BaseEntity
{
    #region Constructors
    public Company()
    {

    }

    public Company(int id, string name, string description)
    {
        Id = id;
        Name = name;
        Description = description;
    }
    #endregion

    #region Properties
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Description { get; set; }
    [DataMember]
    public List<Industry> IndustryList { get; set;}
    #endregion

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

1 Comment

This solution doesn't work, the problem is not on the deseriazaltion itself. It's the json don't contain [] for the industry list because they have only one element in that list. So in the deserialization progress, I get this exception: Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[StockOverflow.Client.Logic.Models.Entities.Industry]'.

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.