0

I have json string like this:

{
    "data": [
        {
            "id": 1,
            "name": "Bitcoin",
            "symbol": "BTC",
            ...
            "quote": {
                "USD": {
                    "price": 9283.92,
                    "volume_24h": 7155680000,
                    "percent_change_1h": -0.152774,
                    "percent_change_24h": 0.518894,
                    "market_cap": 158055024432,
                    "last_updated": "2018-08-09T22:53:32.000Z"
                },
                "BTC": {
                    "price": 1,
                    "volume_24h": 772012,
                    "percent_change_1h": 0,
                    "percent_change_24h": 0,
                    "percent_change_7d": 0,
                    "market_cap": 17024600,
                    "last_updated": "2018-08-09T22:53:32.000Z"
                }
            }
        },
        // objects like previous from which i need the data
    ],
    "status": {
        "timestamp": "2018-06-02T22:51:28.209Z",
        ...
    }
}

How do I deserialize it into models like this:

public class MyModel
{
    public string Name { get; set; }
    public string Symbol { get; set; }
    public string Price { get; set; }
    public double Percent_change_1h { get; set; }
    public double Percent_change_24h { get; set; }
    public long Market_cap { get; set; }
    public DateTime Last_updated { get; set; }
}

The field names in the model are the same as the key names in json string.

I'm new to C# and I couldn't find any helpful information about my question, especially because of this specific json string structure. I'll be glad if you direct me any good links about this.

1
  • When you have json string and want to convert it to model classes. Then copy that json and go to a class in C# and click on top Edit menu > Paste Special > Paste Json As Classes. It will generate model for your json string. Commented Mar 18, 2019 at 14:09

3 Answers 3

1

The model seems to be something like this.

public class Model
{
   public List<Datum> data { get; set; }
   public Status status { get; set; }
}

public class Status
{
    public DateTime timestamp { get; set; }
}

public class Datum
{
    public int id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public Quote quote { get; set; }
}
public class Quote
{
    public USD USD { get; set; }
   public BTC BTC { get; set; }
}
public class BTC
{
    public int price { get; set; }
    public int volume_24h { get; set; }
    public int percent_change_1h { get; set; }
    public int percent_change_24h { get; set; }
    public int percent_change_7d { get; set; }
    public int market_cap { get; set; }
    public DateTime last_updated { get; set; }
}

public class USD
{
    public double price { get; set; }
    public object volume_24h { get; set; }
    public double percent_change_1h { get; set; }
    public double percent_change_24h { get; set; }
    public object market_cap { get; set; }
    public DateTime last_updated { get; set; }
}

You can also try creating model on (http://json2csharp.com/) by copying your valid json string. Please let me know if this helps

Sign up to request clarification or add additional context in comments.

Comments

1
  • Bottom line: You can (manually), but that's probably not what you're looking for.
  • Reason: Your model doesn't match the JSON structure, hence "manual"
  • You can use readily available tools in either Visual Studio or VS Code to help you with creating the proper model (e.g. Paste JSON As Code)
  • Once you get the "proper" model/s ready, go over JSON documentation for (de)serializing.

Comments

1

I had to fix some syntax errors on your json, so fixed version is following:

{
    "data": [
      {
        "id": 1,
        "name": "Bitcoin",
        "symbol": "BTC",
        "quote": {
          "USD": {
            "price": 9283.92,
            "volume_24h": 7155680000,
            "percent_change_1h": -0.152774,
            "percent_change_24h": 0.518894,
            "market_cap": 158055024432,
            "last_updated": "2018-08-09T22:53:32.000Z"
          },
          "BTC": {
            "price": 1,
            "volume_24h": 772012,
            "percent_change_1h": 0,
            "percent_change_24h": 0,
            "percent_change_7d": 0,
            "market_cap": 17024600,
            "last_updated": "2018-08-09T22:53:32.000Z"
          }
        }
      }
    ],
    "status": {
        "timestamp": "2018-06-02T22:51:28.209Z"
    }
}

Here is C# model classes matching with previous json:

public class Rootobject
    {
        public Datum[] data { get; set; }
        public Status status { get; set; }
    }

    public class Status
    {
        public DateTime timestamp { get; set; }
    }

    public class Datum
    {
        public int id { get; set; }
        public string name { get; set; }
        public string symbol { get; set; }
        public Quote quote { get; set; }
    }

    public class Quote
    {
        public USD USD { get; set; }
        public BTC BTC { get; set; }
    }

    public class USD
    {
        public float price { get; set; }
        public long volume_24h { get; set; }
        public float percent_change_1h { get; set; }
        public float percent_change_24h { get; set; }
        public long market_cap { get; set; }
        public DateTime last_updated { get; set; }
    }

    public class BTC
    {
        public int price { get; set; }
        public int volume_24h { get; set; }
        public int percent_change_1h { get; set; }
        public int percent_change_24h { get; set; }
        public int percent_change_7d { get; set; }
        public int market_cap { get; set; }
        public DateTime last_updated { get; set; }
    }

Here is code snippet which you can use when deserializing your json. This snippet uses Json.NET-library.

var obj = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("object.json"));

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.