Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do have these JSON text to deserialize (they are part of bigger structure, just to keep it simple):

{"name":"whatever","value":0}
{"name":"objectProperty","value":{"A":1,"B":2,"C":3,"D":4}}


when I do want to deserialize into the C# object:
C#
public class Rootobject
{
    public string name { get; set; }
    public int value { get; set; }
}


I will get in 1st case:
whatever : 0

in the second case I get:
objectProperty : {object}


the problem is that I'm not able to get any value from the
{object}
.

when I do use this C# class to deserialize:
C#
public class Rootobject
{
    public string name { get; set; }
    public Value value { get; set; }
}

public class Value
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    public int D { get; set; }
}


I will get an error:
Expecting state 'Element'.. Encountered 'Text'  with name '', namespace ''.


Is there a way how to tell deserializer to which class it should deserialize? Or any idea how to solve this problem?

thx,
R.
Posted

Add
C#
[DataContract]
annotation to your class and
C#
[DataMember]
annotation to you class properties
C#
[DataContract]
    internal class Person
    {
        [DataMember]
        internal string name;

        [DataMember]
        internal int age;
    }
 
Share this answer
 
Comments
lumiklumik 19-Nov-15 7:48am    
Sorry you probably didn't understand the issue. I do have JSON sipmple object with just name and value property

C# representation:

public class MyJSON
{
public string name;
public object value;
}

now here is the JSON content I want to deserialize

1st scenario:
{"name":"whatever","value":0}
2nd scenario:
{"name":"objectProperty","value":{"A":1,"B":2,"C":3,"D":4}}

in 1st scenario all works fine in the 2nd scenario value holds just a {object} with nothing inside and I'm not able to persuate serializer to return in value something usable for the 2nd scenario
Solution for me is to use http://www.newtonsoft.com/json[^] instead of standard system runtime serialization.

this library puts "complex content" in string representation which is enough for me for now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900