0

I have string of this kind

string JsonDemo = @"{'status':'OK','articles':[{'key1':'value'},'Maqbool']}";

I want to get values of status, articles, Key1 in seprate varriables in C# window Form application.

I know JSON.Net can be helpful for me but I am not getting what can be the exact code for doing it.

2 Answers 2

1

You can json2csharp.com or visual studio itself has paste special option to generate the class against the json being provided:

public class RootObject
{
    public string status { get; set; }
    public List<object> articles { get; set; }
}

and then we can deserialize it using NewtonSoft.Json library :

var result = JsonConvert.DeserializeObject<RootObject>(JsonDemo);
Sign up to request clarification or add additional context in comments.

Comments

0

Simplest way would be using the JObject class. You could deserialize it to a custom class too. articles is an array, that's why I'm retrieving the first element {'key1':'value'} with index 0.

var obj = JObject.Parse(JsonDemo);
string status = obj["status"].Value<string>();
string key = obj["articles"][0]["key1"].Value<string>();

1 Comment

Thanks a lot man a simple Jobject and all problems solved...thanks again.

Your Answer

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