Abd El Moneim Mohamed
Abd El Moneim Mohamed

Reputation: 11

JsonConvert.DeserializeObject convert json to c# class

all what i want to make class that have properties slimier to this json.

{"code":200,"detected":{"lang":"en"},"lang":"en-ar","text":["hello"]} 

i tried many times please help

Upvotes: 0

Views: 227

Answers (3)

user5766999
user5766999

Reputation:

I find json2csharp very helpful in converting json samples to C# classes/pocos which I then use with Json.NET.

Upvotes: 1

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

If you're using Visual Studio 2013 Update 2 and above, you'll find that it can generate this JSON-compatible class for you.

Just copy a sample JSON structure (like the one you posted here), go to any .cs file, and go to Edit -> Paste Special -> Paste JSON as Classes. VS will automatically generate a class for you that your JSON can be deserialized into.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

The following model should work:

public class MyModel
{
    public int Code { get; set; }
    public Detected Detected { get; set; }
    public string Lang { get; set; }
    public string[] Text { get; set; }
}

public class Detected
{
    public string Lang { get; set; }
}

and then you can deserialize to it:

string json = ...
var model = JsonConvert.DeserializeObject<MyModel>(json);

Upvotes: 2

Related Questions