0

Dearl all I have a json string with format line

  "rain":{"3h":0.46}

In this case , 'rain' have '3h' object but with this name is dynamic so i can't deserialize object it with JsonConvert , so what are define in class with this name ?

Json2Csharp give me this code

    public class Rain
{
    public double __invalid_name__3h { get; set; }
}

1 Answer 1

2

Objects in javascript (and thus JSON) are simply dictionaries. Since you don't know the property name (and more importantly, since it begins with a number which is forbidden in C#), you can simply use a dictionary:

public class MyThing 
{
    public Dictionary<string, double> rain { get; set; }
}

However, your JSON is incomplete. It should be:

{"rain":{"3h":0.46}}

Fully working:

void Main()
{
    var json =  @"{""rain"":{""3h"":0.46}}";
    var result = JsonConvert.DeserializeObject<MyThing>(json);
}

public class MyThing
{
    public Dictionary<string, double> rain { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.