2

I'm trying to get values for the "response" object from the JSON below. I have a problem because I don't know how to get the data from JSON. I'm new in this.

{
    "error": false,
    "response": {
        "id": 6818,
        "name": "Krashnz",
        "avatar": "https:\/\/static.truckersmp.com\/avatarsN\/6818.1538179228.png",
        "smallAvatar": "https:\/\/static.truckersmp.com\/avatarsN\/small\/6818.1538179228.png",
        "joinDate": "2014-11-14 01:33:03",
        "steamID64": 76561198046080290,
        "steamID": "76561198046080290",
        "groupName": "Developer",
        "groupID": 2,
        "banned": false,
        "bannedUntil": null,
        "displayBans": false,
        "permissions": {
            "isGameAdmin": true,
            "showDetailedOnWebMaps": false
        },
        "vtc": {
            "id": 1,
            "name": "TruckersMP Developers",
            "tag": "TMP-DEV",
            "inVTC": true,
            "memberID": 1579
        }
    }
}

How i can do it? Im working in C#

1
  • 2
    Hi, could you please share the code you use for extracting the response from the above JSON ? Thanks Commented Oct 4, 2019 at 12:59

1 Answer 1

3

You can use a tool like json2csharp to convert the json to C# classes. Then use Newtonsoft.Json nuget package to deserialize the json into your class.

Create the classes below. Then you can deserialise like:

var json = // your json string
var root = JsonConvert.DeserializeObject<RootObject>(json);
var response = root.Response;


// classes below
public class Permissions
{
    public bool isGameAdmin { get; set; }
    public bool showDetailedOnWebMaps { get; set; }
}

public class Vtc
{
    public int id { get; set; }
    public string name { get; set; }
    public string tag { get; set; }
    public bool inVTC { get; set; }
    public int memberID { get; set; }
}

public class Response
{
    public int id { get; set; }
    public string name { get; set; }
    public string avatar { get; set; }
    public string smallAvatar { get; set; }
    public string joinDate { get; set; }
    public long steamID64 { get; set; }
    public string steamID { get; set; }
    public string groupName { get; set; }
    public int groupID { get; set; }
    public bool banned { get; set; }
    public object bannedUntil { get; set; }
    public bool displayBans { get; set; }
    public Permissions permissions { get; set; }
    public Vtc vtc { get; set; }
}

public class RootObject
{
    public bool error { get; set; }
    public Response response { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, glad I could help!

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.