在C#中反序列化json-java对象

本文关键字:json-java 对象 反序列化 | 更新日期: 2023-09-27 18:28:39

我正试图使用Json Serializer在c#中反序列化Java Json对象,c#中存在与我在Java中相同的对象,但我得到了以下错误:

{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?"}]},
  "answerswerses":{"@type":"java.util.ArrayList","@items":[{"@type":"concep.selenium.Dynamic.Attributes.AnswersAttributes","Answer":"Single"},
  {"@type":"concep.selenium.Dynamic.Attributes.AnswersAttributes","Answer":"1451567316email@concep.com"},
  {"@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)上。有什么建议吗。

在C#中反序列化json-java对象

试试这个:

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

我解决了这个问题,但在更改用于在Java中序列化Json对象的lib时,我使用了Json.io,它包含了所有这些不必要的类名和注释。现在我是谷歌。Gson,它就像一个魅力!!