2016-01-02 3 views
1

Я пытаюсь десериализации объекта Java JSon в C# с использованием JSon Serializer, одни и те же объекты, я имею в Java существует в C#, но я получаю следующее сообщение об ошибке:Deserialize объект JSON Java в C#

{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.

вот мой Json объект:

{ 
 
"@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"}]} 
 
    }
и вот мой объект:

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


} 

Я подозреваю, что проблема заключается в декларации списка в Java (ArrayList). любое предложение пожалуйста.

ответ

0

Я исправил проблему, но изменяя LIB, что я использовал для сериализации моего объекта JSON в Java, я использовал Json.io, которые он включает в себя все эти ненужные классы имя и аннотацию , и теперь я google.Gson, и это работает как шарм !!

1

Попробуйте это:

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"); 
+0

У меня такая же ошибка –