//这是Json结果的副本:
//----------------------- 前面的json结果,请忽视 ------------{"result_ok":没错,"total_count":"4","页面":1、"total_pages":1、"results_per_page":50,"数据":({"id":"1","contact_id":","状态":"完成","is_test_data":"1"," datessubmitted ": "2011-12-09 02:07:33","问题(2)":"维克多","问题(11)":",[question(11), question_pipe(": "),"[url(":"f299f5ef90291d40543fc731bb1fd755","(变量(":"0","变量(5)":"10009"}]}
//----------------------- 前面的json结果,请忽视 ------------
完整json上下文:
{"result_ok":true,"total_count":"4","page":1,"total_pages":1,"results_per_page":50,"data":[{"id":"1","contact_id":"","status":"Complete","is_test_data":"1","datesubmitted":"2011-12-09 02:07:33","[question(2)]":"Victor","[question(4), option(10001)]":"Oral Roberts","[question(4), option(10002)]":"","[question(4), option(10003)]":"Kansas St","[question(4), option(10004)]":"","[question(4), option(10005)]":"Notre Dame","[question(4), option(10007)]":"","[question(4), option(10008)]":"","[question(5)]":"Black pattern","[question(6), option(10012)]":"Logo1.gif","[question(6), option(10013)]":"Logo3.jpg","[question(6), option(10014)]":"","[question(6), option(10016)]":"","[question(8), question_pipe("Kobe Bryant")]":"Man","[question(8), question_pipe("Miao li Jie")]":"Woman","[question(8), question_pipe("Yao Min")]":"Man","[question(9), question_pipe("Kobe Bryant")]":"NBA","[question(9), question_pipe("Miao li Jie")]":"WCBA","[question(9), question_pipe("Yao Min")]":"NBA","[question(10), option(10024), question_pipe("Kobe Bryant")]":"Average Height","[question(10), option(10025), question_pipe("Kobe Bryant")]":"Muscle","[question(10), option(10026), question_pipe("Kobe Bryant")]":"","[question(10), option(10027), question_pipe("Kobe Bryant")]":"","[question(10), option(10025), question_pipe("Miao li Jie")]":"Muscle","[question(10), option(10026), question_pipe("Miao li Jie")]":"","[question(10), option(10024), question_pipe("Yao Min")]":"","[question(10), option(10025), question_pipe("Yao Min")]":"Muscle","[question(10), option(10027), question_pipe("Yao Min")]":"","[question(11)]":"","[question(11), question_pipe("Kobe Bryant")]":"","[question(11), question_pipe("Miao li Jie")]":"","[question(11), question_pipe("Yao Min")]":"","[question(13)]":"20-99","[question(15)]":"Furniture","[question(16), option(10044)]":"","[question(17)]":"","[question(18), option(10053)]":"","[question(18), option(10054)]":"","[question(18), option(10056)]":"KINWAI","[url("id")]":"746788","[url("crc")]":"f299f5ef90291d40543fc731bb1fd755","[variable("STANDARD_IP")]":"","[variable("STANDARD_LONG")]":"","[variable("STANDARD_LAT")]":"","[variable("STANDARD_GEOCOUNTRY")]":"","[variable("STANDARD_GEOCITY")]":"","[variable("STANDARD_GEOREGION")]":"","[variable("STANDARD_GEOPOSTAL")]":"","[variable("STANDARD_RESPONSETIME")]":"","[variable("STANDARD_COMMENTS")]":"","[variable("STANDARD_GEODMA")]":"0","[variable(5)]":"10009","[variable(8)]":"","[variable(8), question_pipe("Kobe Bryant")]":"10017","[variable(8), question_pipe("Miao li Jie")]":"10018","[variable(8), question_pipe("Yao Min")]":"10017","[variable(9)]":"","[variable(9), question_pipe("Kobe Bryant")]":"10019","[variable(9), question_pipe("Miao li Jie")]":"10023","[variable(9), question_pipe("Yao Min")]":"10019","[variable(13)]":"10031","[variable(15)]":"10042","[variable(17)]":""},{"id":"2","contact_id":"","status":"Partial","is_test_data":"1","datesubmitted":"2011-12-09 02:08:11","[question(2)]":"Jerry","[question(4), option(10001)]":"","[question(4), option(10002)]":"","[question(4), option(10003)]":"","[question(4), option(10004)]":"","[question(4), option(10005)]":"","[question(4), option(10007)]":"Harvard","[question(4), option(10008)]":"","[question(5)]":"Black pattern","[question(6), option(10012)]":"","[question(6), option(10013)]":"Logo3.jpg","[question(6), option(10014)]":"Logo4.jpg","[question(6), option(10016)]":"","[question(8), question_pipe("Kobe Bryant")]":"Man","[question(8), question_pipe("Miao li Jie")]":"Woman","[question(8), question_pipe("Yao Min")]":"Man","[question(9), question_pipe("Kobe Bryant")]":"NBA","[question(9), question_pipe("Miao li Jie")]":"WCBA","[question(9), question_pipe("Yao Min")]":"CBA","[question(10), option(10024), question_pipe("Kobe Bryant")]":"","[question(10), option(10025), question_pipe("Kobe Bryant")]":"","[question(10), option(10026), question_pipe("Kobe Bryant")]":"","[question(10), option(10027), question_pipe("Kobe Bryant")]":"","[question(10), option(10025), question_pipe("Miao li Jie")]":"","[question(10), option(10026), question_pipe("Miao li Jie")]":"","[question(10), option(10024), question_pipe("Yao Min")]":"","[question(10), option(10025), question_pipe("Yao Min")]":"","[question(10), option(10027), question_pipe("Yao Min")]":"","[question(11)]":"","[question(11), question_pipe("Kobe Bryant")]":"","[question(11), question_pipe("Miao li Jie")]":"","[question(11), question_pipe("Yao Min")]":"","[question(13)]":"100-499","[question(15)]":"Clothing","[question(16), option(10044)]":"","[question(17)]":"Dark","[question(18), option(10053)]":"","[question(18), option(10054)]":"","[question(18), option(10056)]":"","[url("id")]":"746788","[url("crc")]":"f299f5ef90291d40543fc731bb1fd755","[variable("STANDARD_IP")]":"","[variable("STANDARD_LONG")]":"","[variable("STANDARD_LAT")]":"","[variable("STANDARD_GEOCOUNTRY")]":"","[variable("STANDARD_GEOCITY")]":"","[variable("STANDARD_GEOREGION")]":"","[variable("STANDARD_GEOPOSTAL")]":"","[variable("STANDARD_RESPONSETIME")]":"","[variable("STANDARD_COMMENTS")]":"","[variable("STANDARD_GEODMA")]":"0","[variable(5)]":"10009","[variable(8)]":"","[variable(8), question_pipe("Kobe Bryant")]":"10017","[variable(8), question_pipe("Miao li Jie")]":"10018","[variable(8), question_pipe("Yao Min")]":"10017","[variable(9)]":"","[variable(9), question_pipe("Kobe Bryant")]":"10019","[variable(9), question_pipe("Miao li Jie")]":"10023","[variable(9), question_pipe("Yao Min")]":"10020","[variable(13)]":"10032","[variable(15)]":"10040","[variable(17)]":"10050"},{"id":"3","contact_id":"","status":"Complete","is_test_data":"1","datesubmitted":"2011-12-09 02:19:42","[question(2)]":"todd","[question(4), option(10001)]":"","[question(4), option(10002)]":"","[question(4), option(10003)]":"Kansas St","[question(4), option(10004)]":"West Virginia","[question(4), option(10005)]":"","[question(4), option(10007)]":"","[question(4), option(10008)]":"Connecticut","[question(5)]":"Orange pattern","[question(6), option(10012)]":"Logo1.gif","[question(6), option(10013)]":"Logo3.jpg","[question(6), option(10014)]":"","[question(6), option(10016)]":"Logo2.jpg","[question(8), question_pipe("Kobe Bryant")]":"Woman","[question(8), question_pipe("Miao li Jie")]":"Woman","[question(8), question_pipe("Yao Min")]":"Woman","[question(9), question_pipe("Kobe Bryant")]":"ACB","[question(9), question_pipe("Miao li Jie")]":"EML","[question(9), question_pipe("Yao Min")]":"CBA","[question(10), option(10024), question_pipe("Kobe Bryant")]":"Average Height","[question(10), option(10025), question_pipe("Kobe Bryant")]":"Muscle","[question(10), option(10026), question_pipe("Kobe Bryant")]":"Beard","[question(10), option(10027), question_pipe("Kobe Bryant")]":"WaveHairs","[question(10), option(10025), question_pipe("Miao li Jie")]":"Muscle","[question(10), option(10026), question_pipe("Miao li Jie")]":"Beard","[question(10), option(10024), question_pipe("Yao Min")]":"Average Height","[question(10), option(10025), question_pipe("Yao Min")]":"","[question(10), option(10027), question_pipe("Yao Min")]":"WaveHairs","[question(11)]":"","[question(11), question_pipe("Kobe Bryant")]":"ddd","[question(11), question_pipe("Miao li Jie")]":"dd","[question(11), question_pipe("Yao Min")]":"ddd","[question(13)]":"10-19","[question(15)]":"Furniture","[question(16), option(10044)]":"","[question(17)]":"","[question(18), option(10053)]":"QUANU","[question(18), option(10054)]":"HUARI","[question(18), option(10056)]":"","[url("id")]":"746788","[url("crc")]":"f299f5ef90291d40543fc731bb1fd755","[variable("STANDARD_IP")]":"","[variable("STANDARD_LONG")]":"","[variable("STANDARD_LAT")]":"","[variable("STANDARD_GEOCOUNTRY")]":"","[variable("STANDARD_GEOCITY")]":"","[variable("STANDARD_GEOREGION")]":"","[variable("STANDARD_GEOPOSTAL")]":"","[variable("STANDARD_RESPONSETIME")]":"","[variable("STANDARD_COMMENTS")]":"","[variable("STANDARD_GEODMA")]":"0","[variable(5)]":"10010","[variable(8)]":"","[variable(8), question_pipe("Kobe Bryant")]":"10018","[variable(8), question_pipe("Miao li Jie")]":"10018","[variable(8), question_pipe("Yao Min")]":"10018","[variable(9)]":"","[variable(9), question_pipe("Kobe Bryant")]":"10021","[variable(9), question_pipe("Miao li Jie")]":"10022","[variable(9), question_pipe("Yao Min")]":"10020","[variable(13)]":"10030","[variable(15)]":"10042","[variable(17)]":""},{"id":"4","contact_id":"","status":"Partial","is_test_data":"1","datesubmitted":"2011-12-09 02:21:10","[question(2)]":"suan","[question(4), option(10001)]":"Oral Roberts","[question(4), option(10002)]":"Oklahoma","[question(4), option(10003)]":"","[question(4), option(10004)]":"","[question(4), option(10005)]":"","[question(4), option(10007)]":"","[question(4), option(10008)]":"","[question(5)]":"Red pattern","[question(6), option(10012)]":"Logo1.gif","[question(6), option(10013)]":"","[question(6), option(10014)]":"","[question(6), option(10016)]":"","[question(8), question_pipe("Kobe Bryant")]":"Man","[question(8), question_pipe("Miao li Jie")]":"Man","[question(8), question_pipe("Yao Min")]":"Man","[question(9), question_pipe("Kobe Bryant")]":"NBA","[question(9), question_pipe("Miao li Jie")]":"NBA","[question(9), question_pipe("Yao Min")]":"NBA","[question(10), option(10024), question_pipe("Kobe Bryant")]":"","[question(10), option(10025), question_pipe("Kobe Bryant")]":"Muscle","[question(10), option(10026), question_pipe("Kobe Bryant")]":"","[question(10), option(10027), question_pipe("Kobe Bryant")]":"WaveHairs","[question(10), option(10025), question_pipe("Miao li Jie")]":"Muscle","[question(10), option(10026), question_pipe("Miao li Jie")]":"","[question(10), option(10024), question_pipe("Yao Min")]":"","[question(10), option(10025), question_pipe("Yao Min")]":"Muscle","[question(10), option(10027), question_pipe("Yao Min")]":"","[question(11)]":"","[question(11), question_pipe("Kobe Bryant")]":"","[question(11), question_pipe("Miao li Jie")]":"","[question(11), question_pipe("Yao Min")]":"china","[question(13)]":"20-99","[question(15)]":"House-hold","[question(16), option(10044)]":"TV","[question(17)]":"","[question(18), option(10053)]":"","[question(18), option(10054)]":"","[question(18), option(10056)]":"","[url("id")]":"746788","[url("crc")]":"f299f5ef90291d40543fc731bb1fd755","[variable("STANDARD_IP")]":"","[variable("STANDARD_LONG")]":"","[variable("STANDARD_LAT")]":"","[variable("STANDARD_GEOCOUNTRY")]":"","[variable("STANDARD_GEOCITY")]":"","[variable("STANDARD_GEOREGION")]":"","[variable("STANDARD_GEOPOSTAL")]":"","[variable("STANDARD_RESPONSETIME")]":"","[variable("STANDARD_COMMENTS")]":"","[variable("STANDARD_GEODMA")]":"0","[variable(5)]":"10011","[variable(8)]":"","[variable(8), question_pipe("Kobe Bryant")]":"10017","[variable(8), question_pipe("Miao li Jie")]":"10017","[variable(8), question_pipe("Yao Min")]":"10017","[variable(9)]":"","[variable(9), question_pipe("Kobe Bryant")]":"10019","[variable(9), question_pipe("Miao li Jie")]":"10019","[variable(9), question_pipe("Yao Min")]":"10019","[variable(13)]":"10031","[variable(15)]":"10039","[variable(17)]":""}]}
我试过实体类:
//实体类[JsonObject(MemberSerialization.OptIn)]
[JsonConverter(typeof(SurveryResponseConverter))]
public class SurveyResponse
{
/// <summary>
/// Reponse ID
/// </summary>
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "id")]
public string id { get; set; }
/// <summary>
/// Contact id
/// </summary>
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "contact_id")]
public string contact_id { get; set; }
/// <summary>
/// Reponse status
/// </summary>
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "status")]
public string status { get; set; }
/// <summary>
/// Is test data?
/// </summary>
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "is_test_data")]
public bool is_test_data { get; set; }
/// <summary>
/// when submitt the response?
/// </summary>
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "date_submitted")]
public string date_submitted { get; set; }
[JsonConverter(typeof(CustomConverter))]
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, Question> Questions { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class Option
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Key { get; set; }
[JsonProperty]
public string Value { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, QuestionPipe> Options { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class Question
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore )]
public string Key { get; set; }
[JsonProperty]
public string Value { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore )]
public Dictionary<string, Option> Options { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, QuestionPipe> QuestionPipes { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class QuestionPipe
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Key { get; set; }
[JsonProperty]
public string Value { get; set; }
[JsonProperty]
public string Answer { get; set; }
}
//转换器类public class SurveryResponseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(SurveyResponse);
}
public override bool CanRead
{
get { return true; }
}
public override object ReadJson(JsonReader jsonReader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = (SurveyResponse)existingValue;
if (value == null)
{
value = new SurveyResponse();
value.Questions = new Dictionary<string, Question>();
}
//string readResult = "{n";
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonToken.PropertyName)
{
var name = jsonReader.Value.ToString();
if (name.StartsWith("[question("))
{
jsonReader.Read();//read the value
Regex regexQuestion = null ;//omitted for saving context
Regex regexQuestionAndOption = null;//omitted for saving context
Regex regexQuestionAndQuestionPipe = null;//omitted for saving context
Regex regexQuestionAndOptionAndQuestionPipe =null;//omitted for saving context
bool isMatchQuestion, isMatchQuestionAndOption, isMatchQuestionAndPipe, isMatchQuestionAndOptionAndPipe;
isMatchQuestion = regexQuestion.IsMatch(name);
isMatchQuestionAndOption = regexQuestionAndOption.IsMatch(name);
isMatchQuestionAndPipe = regexQuestionAndQuestionPipe.IsMatch(name);
isMatchQuestionAndOptionAndPipe = regexQuestionAndOptionAndQuestionPipe.IsMatch(name);
//bool isMatchQuestionAndOption, isMatchQuestionAndPipe, isMatchQuestionAndOptionAndPipe;
if (isMatchQuestion)
{
//eg.[question(int)]
int index = int.Parse(name.Substring(10, name.IndexOf(')') - 10));
string strValue = serializer.Deserialize<string>(jsonReader);
Question questionValue = new Question();
questionValue.Key = index.ToString();
questionValue.Value = strValue;
value.Questions[index.ToString()] = questionValue;
}
else if (isMatchQuestionAndOption)
{
//eg.[question(4), option(10001)]
string questionKey = name.Substring(10, name.IndexOf(')') - 10);
string questionOptionValue = serializer.Deserialize<string>(jsonReader);
string questionOptionKey = name.Substring(name.LastIndexOf('(')+1, name.LastIndexOf(')') - name.LastIndexOf('(')-1);
Option optionValue = new Option();
optionValue.Key = questionOptionKey;
optionValue.Value = serializer.Deserialize<string>(jsonReader);
}
else if (isMatchQuestionAndPipe)
{
//eg.[question(10),question_pipe(" some description of question")]
string questionKey = name.Substring(10, name.IndexOf(')') - 10);
QuestionPipe questionPipe = new QuestionPipe();
questionPipe.Key = "TODO";
questionPipe.Value = name.Substring(name.LastIndexOf('(') + 1, name.LastIndexOf(')') - name.LastIndexOf('(') - 1);
questionPipe.Answer = serializer.Deserialize<string>(jsonReader);
}
else if (isMatchQuestionAndOptionAndPipe)
{
//eg.[question(10), option(10024), question_pipe(" some description of question")]
string questionKey = name.Substring(10, name.IndexOf(')') - 10);
string subName = name.Substring(name.IndexOf('('));
Option optionValue = new Option();
optionValue.Key = subName.Substring(1, subName.IndexOf(')') - 1);
QuestionPipe questionPipe = new QuestionPipe();
questionPipe.Key = "TODO";
questionPipe.Value = name.Substring(name.LastIndexOf('(') + 1, name.LastIndexOf(')') - name.LastIndexOf('(') - 1);
questionPipe.Answer = serializer.Deserialize<string>(jsonReader);
}
}
else if (name.StartsWith("[variable("))
{
//TODO
}
else if (name.StartsWith("[url("))
{
//TODO
}
else
{
var property = typeof(SurveyResponse).GetProperty(name);
try
{
//这里将出现一个异常:在反序列化对象时意外的令牌:PropertyNamevar propertyValue = serializer。反序列化(jsonReader property.PropertyType);}catch(异常ex){字符串errMsg = ex.Message;扔;}}}}
return value;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
你能给我一些建议吗?大家圣诞快乐^_^
JSON无效,围绕question_pipe的事情开始变得疯狂。这里有一个在线验证器的链接,可以帮助您清理它。http://jsonlint.com/
希望在您的情况下,您可以控制对象的序列化方式,否则您可能必须在尝试反序列化之前清除/转义字符串中的非法字符。希望这对你有帮助,节日快乐!
阅读完整的JSON示例后,我很好奇您的对象是如何序列化的。是否有自定义序列化器来获取JSON?原因是,JSON只是一个非常简单的对象符号。您会发现,很多时候,JSON被反序列化为Dictionary,这意味着它是一个简单的键值对。你所列出的JSON比这复杂得多,我想知道这是不是你的问题。
例如…让我们看一个简单的对象:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person Sibling { get; set; }
}
一个有效的JSON表示应该是:
{
"FirstName": "John",
"LastName": "Doe",
"Sibling":
{
"FirstName": "Jane",
"LastName": "Doe",
"Sibling": null
}
}
非常简单,表示。每个属性名都有一个字符串,然后是相应的值。在您的情况下,您有像[question(4), option(10001)]这样的属性名,我不确定任何标准序列化程序都知道如何处理它。它只是寻找一个一对一的匹配,没有办法创建一个属性名称[question(4), option(10001)]本身。里面肯定有无效字符,那么房产的名称应该是什么呢?
我认为这里有一些明确的设计考虑。我希望这有助于解释解析器抛出错误的原因。也许我没有完全理解你想要完成的任务,但我希望我的解释能帮助你对潜在的问题和概念设计的考虑有所帮助。
节日快乐!