-3

I have the following json structure(this is beautified). I'm having trouble with defining class definitions for the following things below(array in array?). And how would I loop through each "row" and compare if b->iscritical for example would equal to 1.

[
  {
    "b": {
      "iscritical": 0,
      "value": "fiber4/2/1"
    },
    "c": {
      "iscritical": 0,
      "value": 1990
    },
    "dd": {
      "iscritical": 0,
      "value": {
        "dname": "Texas",
        "mdomain": "fiber4/2/1",
        "text": "Texas FTTH"
      }
    }
  },
  {
    "b": {
      "iscritical": 0,
      "value": "fiber4/2/2"
    },
    "c": {
      "iscritical": 0,
      "value": 1991
    },
    "dd": {
      "iscritical": 0,
      "value": {
        "dname": "Texas",
        "mdomain": "fiber4/2/2",
        "text": "Texas FTTH"
      }
    }
  }
]

Thank you in advance!

  • 1
    Does this answer your question? [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) – Anderson Green Jun 08 '20 at 17:44
  • In Visual Studio, you can paste the JSON into classes (Edit, Paste Special, Paste Json As Classes – Jawad Jun 08 '20 at 17:55

1 Answers1

1

Your question is pretty vague. Your fist problem is how to deserialize this JSON string into objects.

This is an array of smaller objects, which also contain other objects as properties. You could define your classes like:

public class Example
  {
    [JsonProperty("b")]
    public B B { get; set; }

    [JsonProperty("c")]
    public C C { get; set; }

    [JsonProperty("dd")]
    public Dd Dd { get; set; }
  }

  public class B
  {
    [JsonProperty("iscritical")]
    public long Iscritical { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
  }

  public class C
  {
    [JsonProperty("iscritical")]
    public long Iscritical { get; set; }

    [JsonProperty("value")]
    public long Value { get; set; }
  }

  public class Dd
  {
    [JsonProperty("iscritical")]
    public long Iscritical { get; set; }

    [JsonProperty("value")]
    public Value Value { get; set; }
  }

  public class Value
  {
    [JsonProperty("dname")]
    public string Dname { get; set; }

    [JsonProperty("mdomain")]
    public string Mdomain { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }
  }

And when you have your JSON as a string, you can deserialize it using Newtonsoft.JSON, like so:

IEnumerable<Example> values = JsonConvert.DeserializeObject<Example[]>(json);

Sotiris Panopoulos
  • 1,523
  • 1
  • 13
  • 18