1

I am trying to de-serialize a Java Json object in c# using Json Serializer, the same objects I have in Java exist in C#, but i get the following error:

{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Dynamic4ClientConsumer.Attributes.SurveyResponseAttributes]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

here is my Json object:

{
"@type":"concep.selenium.Dynamic.Attributes.SurveyAttributes","surveyType":"1451567316Feedback FormNokia 925 Launch","surveyID":null,"campaignType":"Nokia 925 Launch","surveyStatus":null,"SurveyResponse":
  {"@type":"java.util.ArrayList"},
  "CampaignResponseAttribute":{"@type":"java.util.ArrayList"},
  "questions":{"@type":"java.util.ArrayList","@items":[{"@type":"concep.selenium.Dynamic.Attributes.QuestionsAttributes","question":"Whats your Martial Status?"},
  {"@type":"concep.selenium.Dynamic.Attributes.QuestionsAttributes","question":"Whats your Email?"},
  {"@type":"concep.selenium.Dynamic.Attributes.QuestionsAttributes","question":"Whats your Last Name?"}]},
  "answerses":{"@type":"java.util.ArrayList","@items":[{"@type":"concep.selenium.Dynamic.Attributes.AnswersAttributes","Answer":"Single"},
  {"@type":"concep.selenium.Dynamic.Attributes.AnswersAttributes","Answer":"[email protected]"},
  {"@type":"concep.selenium.Dynamic.Attributes.AnswersAttributes","Answer":"1451567316lastName"}]}
  }
and here is my object:

  public class SurveyAttributes
{
    public SurveyAttributes()
    {
        SurveyResponse = new List<SurveyResponseAttributes>();
        CampaignResponseAttribute = new List<CampaignResponseAttribute>();
        Answerses = new List<AnswersAttributes>();
        Questions = new List<QuestionsAttributes>();
    }

    public string SurveyType { get; set; }
    public string SurveyId { get; set; }
    public string CampaignType { get; set; }
    public string SurveyStatus { get; set; }
    public List<SurveyResponseAttributes> SurveyResponse { get; set; }
    public List<CampaignResponseAttribute> CampaignResponseAttribute { get; set; }
    public List<QuestionsAttributes> Questions { get; set; }
    public List<AnswersAttributes> Answerses{ get; set; }


}

I suspect that the problem is the list declaration in Java(ArrayList). any suggestion please.

2 Answers 2

1

Try this:

  public class SurveyAttributes
{
    public SurveyAttributes()
    {
        SurveyResponse = new List<SurveyResponseAttributes>();
        CampaignResponseAttribute = new List<CampaignResponseAttribute>();
        Answerses = new List<AnswersAttributes>();
        Questions = new List<QuestionsAttributes>();
    }

    public string SurveyType { get; set; }
    public string SurveyId { get; set; }
    public string CampaignType { get; set; }
    public string SurveyStatus { get; set; }
    public Questions Questions { get; set; }
}

public class Questions
{
  [JsonProperty(PropertyName = "@items")]
  public List<Question> Questions {get;set;}
}

public class Question
{
  public string Question { get; set; }
}

var result = JsonConvert.DeserializeObject<SurveyAttributes>("// your data here");
Sign up to request clarification or add additional context in comments.

Comments

0

I fixed the problem but changing the lib that I was using for serializing my Json object in Java, I was using Json.io which it includes all these unnecessary classes name and annotation. and now I am google.Gson, and it works like a charm!!

Comments

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.