I'm trying to desearialize the following using Newtonsoft.Json
{
"max_id_str":"1234567",
"results":[{
"created_at":"Tue, 21 May 2013 03:06:23 +0000",
"from_user":"Name Here",
"from_user_id":33333,
"text":"THE TEXT GOES HERE"
}],
"results_per_page":1,
"since_id":0,
"since_id_str":"0"
}
I can retrieve the max_id_str using desearialization but cannot get any of the data in "results"
Here's the code I have
public class tweet
{
public string max_id_str { get; set; }
public string text{ get; set; }
public string results_per_page{ get; set; }
public string since_id { get; set; }
public string since_id_str { get; set; }
}
I then create an object of the class and attempt to desearlize it into the object
tweet t = new tweet();
t = JsonConvert.DeserializeObject<tweet>(e.Result);
Everything but "text" populates? Text's value is null when I output the value. Any ideas how to accomplish what I'm trying?
I'm not sure what you're expecting to get deserializing that JSON string to that type, text
is not a property of the object so there's no reason to expect it to do so like that. text
is a property of the objects within the results
list. You need to map those objects as well and then access the text through the result
objects.
public class tweet
{
public string max_id_str { get; set; }
//public string text{ get; set; }
public List<result> results { get; set; }
public string results_per_page{ get; set; }
public string since_id { get; set; }
public string since_id_str { get; set; }
}
public class result
{
public string created_at { get; set; }
public string from_user { get; set; }
public int from_user_id { get; set; }
public string text { get; set; }
}
If you were trying to use the values within results to determine the value of your text property, you could write a converter to extract the text value. Just add a JsonProperty
and JsonConverter
attribute to your text
property and implement the converter.
public class tweet
{
public string max_id_str { get; set; }
[JsonProperty("results")]
[JsonConverter(typeof(TextPropertyResultExtractorConverter))]
public string text { get; set; }
public string results_per_page{ get; set; }
public string since_id { get; set; }
public string since_id_str { get; set; }
}
public class TextPropertyResultExtractorConverter : JsonConverter
{
public override bool CanConvert(Type type)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var results = (JArray)serializer.Deserialize(reader);
var result = results.First();
return result.Value<string>("text");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
It's because text
is a child element, whereas in your C# object you're having it directly in tweet. You're missing the results
object as a property. There is no way for the deserializer to know where to place those values (there are no corresponding properties).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.