How to convert this json string to jobject

Viewed 1205

How to convert this json string to Jobject

"{\"user\":{\"id\":373697,\"username\":\"luckygirlxxx123\",\"counter_rechecking\":0,\"user_id\":76131,\"fb_id\":\"100047460285611\"}}";

I need it convert to Jobject like this because i need it to post data to website api . Example :

        JObject concac = new JObject();
        concac1["id"] = 373697;
        concac1["username"] = "luckygirlxxx123";
        concac1["counter_rechecking"] = 0;
        concac1["user_id"] = 76131;
        concac1["fb_id"] = "100047460285611";

Because It inside USER that i dont know how to create jobject with that

2 Answers

this code might help you.

    public static void Main(string[] args)
    {
        string jsonData = "{\"user\":{\"id\":373697,\"username\":\"luckygirlxxx123\",\"counter_rechecking\":0,\"user_id\":76131,\"fb_id\":\"100047460285611\"}}";  

        var details = JObject.Parse(jsonData);  
        Console.WriteLine(string.Concat(details["user"]["id"], " " + details["user"]["username"]));
    }

The simplest way is by giving those JSON values to the contracture of the object

public JObject(int id, string username, int user_id ....)

then you declare object and you pass values to it

JObject concac = new JObject(concac1["id"], concac1["username"], concac1["user_id"]..... );

The second way is JSON Serialization And Deserialization In C#

Related