Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON property starting with @ symbol into C# dynamic object?

Tags:

c#

json.net

How to deserialize Json property to dynamic object if it starts with @ symbol.

{
    "@size": "13",
    "text": "some text",
    "Id": 483606
}

I can get id and text properties like this.

dynamic json = JObject.Parse(txt);
string x = json.text;
like image 440
user1429899 Avatar asked Apr 03 '15 12:04

user1429899


People also ask

How do I deserialize an object in JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How deserialize JSON works C#?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it creates a JavaScriptSerializer instance and calls Deserialize() by passing JSON data. It returns a custom object (BlogSites) from JSON data.

What is serialized and deserialized in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

What is Jsonproperty attribute?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.


2 Answers

Assuming you use Json.NET:

public class MyObject
{
    [JsonProperty("@size")]
    public string size { get; set; }

    public string text { get; set; }

    public int Id { get; set; }
}

var result = JsonConvert.DeserializeObject<MyObject>(json);
like image 104
stefankmitph Avatar answered Oct 05 '22 23:10

stefankmitph


Since you can't use @ in a C# token name, you would need to map the @size to something else, like "SizeString" (since it is a string in your JSON above). I use the WCF data contract attribute, but you could use the equivalent JSON attribute

...
[DataMember(Name = "@size")]
public string SizeString { get; set; }
...

Here is an example of how to deserialize the Json string. Maybe you can adapt to your situation, or clarify your question.

...
string j = @"{
            ""@size"": ""13"",
            ""text"": ""some text"",
            ""Id"": 483606
        }";
        MyClass mc = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(j);
...

[DataContract]
public class MyClass
{
    [DataMember(Name="@size")]
    public string SizeString { get; set; }
    [DataMember()]
    public string text { get; set; }
    [DataMember()]
    public int Id { get; set; }
}

If you don't plan loading the Json into a predefined class, you can do the following...

var o = JObject.Parse(j);
var x = o["text"];
var size = o["@size"];
like image 39
Les Avatar answered Oct 05 '22 23:10

Les