gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:41:49 25 4
gpt4 key购买 nike

我正在尝试使用 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?"}]},
"answerses":{"@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)中的列表声明。有什么建议请。

最佳答案

试试这个:

  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 - C# 中反序列化 json java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34565216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com