What kind of string is this? How do I unserialize this string? [duplicate]

This is a serialized string. You can unserialize it with this function: unserialize(), like this: $str=”a:2:{i:0;s:7:”Abogado”;i:1;s:7:”Notario”;}”; print_r(unserialize($str)); Output: Array ( [0] => Abogado [1] => Notario ) Side Note: A quote from the manual: Warning: FALSE is returned both in the case of an error and if unserializing the serialized FALSE value. It is possible … Read more

Can I specify a path in an attribute to map a property in my class to a child property in my JSON?

Well, if you just need a single extra property, one simple approach is to parse your JSON to a JObject, use ToObject() to populate your class from the JObject, and then use SelectToken() to pull in the extra property. So, assuming your class looked something like this: class Person { [JsonProperty(“name”)] public string Name { … Read more

How do I write a custom JSON deserializer for Gson?

I’d take a slightly different approach as follows, so as to minimize “manual” parsing in my code, as unnecessarily doing otherwise somewhat defeats the purpose of why I’d use an API like Gson in the first place. // output: // [User: id=1, name=Jonas, updateDate=2011-03-24 03:35:00.226] // [User: id=5, name=Test, updateDate=2011-05-07 08:31:38.024] // using java.sql.Timestamp public … Read more

Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings. JSON The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name. {“Id”: 1, “Name”: “biofractal”} C# Use JsonConvert.DeserializeObject<dynamic>() to deserialize … Read more

.NET NewtonSoft JSON deserialize map to a different property name

Json.NET – Newtonsoft has a JsonPropertyAttribute which allows you to specify the name of a JSON property, so your code should be: public class TeamScore { [JsonProperty(“eighty_min_score”)] public string EightyMinScore { get; set; } [JsonProperty(“home_or_away”)] public string HomeOrAway { get; set; } [JsonProperty(“score “)] public string Score { get; set; } [JsonProperty(“team_id”)] public string TeamId … Read more

How do I use JSON.NET to deserialize into nested/recursive Dictionary and List?

If you just want a generic method that can handle any arbitrary JSON and convert it into a nested structure of regular .NET types (primitives, Lists and Dictionaries), you can use JSON.Net’s LINQ-to-JSON API to do it: using System.Linq; using Newtonsoft.Json.Linq; public static class JsonHelper { public static object Deserialize(string json) { return ToObject(JToken.Parse(json)); } … Read more