文件内容:{
"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(\": "Man",
"[question(9), question_pipe(\": "NBA",
"[question(10), option(10024), question_pipe(\": "",
"[question(10), option(10025), question_pipe(\": "Muscle",
"[question(10), option(10026), question_pipe(\": "",
"[question(10), option(10027), question_pipe(\": "",
"[question(11)]": "",
"[question(11), question_pipe(\": "",
"[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(\": "f299f5ef90291d40543fc731bb1fd755",
"[variable(\": "0",
"[variable(5)]": "10009",
"[variable(8)]": "",
"[variable(8), question_pipe(\": "10017",
"[variable(9)]": "",
"[variable(9), question_pipe(\": "10019",
"[variable(13)]": "10031",
"[variable(15)]": "10042",
"[variable(17)]": ""
}
]
}我之前已经做的工作: 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 reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = (SurveyResponse)existingValue;
if (value == null)
{
value = new SurveyResponse();
value.Questions = new Dictionary<int, string>();
} // Skip opening {
reader.Read(); while (reader.TokenType == JsonToken.PropertyName)
{
var name = reader.Value.ToString();
reader.Read(); // Here is where you do your magic
if (name.StartsWith("[question("))
{
int index = int.Parse(name.Substring(10, name.IndexOf(')') - 10));
value.Questions[index] = serializer.Deserialize<string>(reader);
}
else if (name.StartsWith("[value("))
{
int index = int.Parse(name.Substring(7, name.IndexOf(')') - 7));
value.Values[index] = serializer.Deserialize<string>(reader);
}
else
{
var property = typeof(SurveyResponse).GetProperty(name);
property.SetValue(value, serializer.Deserialize(reader, property.PropertyType), null);
} // Skip the , or } if we are at the end
reader.Read();
}
return value;
} public override bool CanWrite
{
get { return false; }
} public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}实体类 [JsonObject(MemberSerialization.OptIn)]
[JsonConverter(typeof(SurveryResponseConverter))]
public class SurveyResponse
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "id")]
public string ID { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "contact_id")]
public string ContactId { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "status")]
public string Status { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "is_test_data")]
public bool IsTestData { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "date_submitted")]
public string DateSubmitted { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<int, string> Questions { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<int, string> Values { get; set; }
}测试方法:
SurveyResponse surveyResponseResultBody = obj.DeserializeToObject<SurveyResponse>("data");我是新手,希望大家能给予帮助~
"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(\": "Man",
"[question(9), question_pipe(\": "NBA",
"[question(10), option(10024), question_pipe(\": "",
"[question(10), option(10025), question_pipe(\": "Muscle",
"[question(10), option(10026), question_pipe(\": "",
"[question(10), option(10027), question_pipe(\": "",
"[question(11)]": "",
"[question(11), question_pipe(\": "",
"[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(\": "f299f5ef90291d40543fc731bb1fd755",
"[variable(\": "0",
"[variable(5)]": "10009",
"[variable(8)]": "",
"[variable(8), question_pipe(\": "10017",
"[variable(9)]": "",
"[variable(9), question_pipe(\": "10019",
"[variable(13)]": "10031",
"[variable(15)]": "10042",
"[variable(17)]": ""
}
]
}我之前已经做的工作: 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 reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = (SurveyResponse)existingValue;
if (value == null)
{
value = new SurveyResponse();
value.Questions = new Dictionary<int, string>();
} // Skip opening {
reader.Read(); while (reader.TokenType == JsonToken.PropertyName)
{
var name = reader.Value.ToString();
reader.Read(); // Here is where you do your magic
if (name.StartsWith("[question("))
{
int index = int.Parse(name.Substring(10, name.IndexOf(')') - 10));
value.Questions[index] = serializer.Deserialize<string>(reader);
}
else if (name.StartsWith("[value("))
{
int index = int.Parse(name.Substring(7, name.IndexOf(')') - 7));
value.Values[index] = serializer.Deserialize<string>(reader);
}
else
{
var property = typeof(SurveyResponse).GetProperty(name);
property.SetValue(value, serializer.Deserialize(reader, property.PropertyType), null);
} // Skip the , or } if we are at the end
reader.Read();
}
return value;
} public override bool CanWrite
{
get { return false; }
} public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}实体类 [JsonObject(MemberSerialization.OptIn)]
[JsonConverter(typeof(SurveryResponseConverter))]
public class SurveyResponse
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "id")]
public string ID { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "contact_id")]
public string ContactId { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "status")]
public string Status { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "is_test_data")]
public bool IsTestData { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "date_submitted")]
public string DateSubmitted { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<int, string> Questions { get; set; } [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<int, string> Values { get; set; }
}测试方法:
SurveyResponse surveyResponseResultBody = obj.DeserializeToObject<SurveyResponse>("data");我是新手,希望大家能给予帮助~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货