1

I want to get the subdata in the json data I have, but I could not.

The codes I use are like this

JSON

    {
  "ok": true,
  "result": [
    {
      "update_id": XXXXX,
      "message": {
        "message_id": XXXXX,
        "from": {
          "id": XXXXX,
          "is_bot": false,
          "first_name": "XXXXX",
          "last_name": "XXXXX",
          "username": "XXXXX",
          "language_code": "tr"
        },
        "chat": {
          "id": XXXXX,
          "first_name": "XXXXX",
          "last_name": "XXXXX",
          "username": "XXXXX",
          "type": "XXXXX"
        },
        "date": XXXXX,
        "text": "XXXXX"
      }
    },
    {
      "update_id": XXXXX,
      }
    }
  ]
}

My Code

string link = "xxxxx";
var request = WebRequest.Create(link);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
string rs = reader.ReadToEnd();
dynamic json = JsonConvert.DeserializeObject(rs);
foreach (var item in json)
{
MessageBox.Show(item.result.message.chat.id);
}

My codes want to print the subdata like this, I would be glad if you help. Thank you from now

Yunus Kaya
  • 13
  • 2
  • Use `JsonConvert`. https://stackoverflow.com/questions/18784697/how-to-import-jsonconvert-in-c-sharp-application#18784702 – H. Pauwelyn Jan 16 '21 at 18:53
  • Does this answer your question? [How to import JsonConvert in C# application?](https://stackoverflow.com/questions/18784697/how-to-import-jsonconvert-in-c-sharp-application) – H. Pauwelyn Jan 16 '21 at 18:54

2 Answers2

0

You json is not an array but an object that contains an array of result.

Try this way to get the data you need.

dynamic json = JsonConvert.DeserializeObject(rs);
Console.WriteLine(json["result"][0]["message"]["chat"]["id"].ToString());

This is definitely not the best way to go about it so I would recommend creating a class that represents the json object and deserialize to it. You can use paste special under edit in visual studio to create class objects for your json. That way you will be able to access it via,

var chatId = json.Result.FirstOrDefault().Message.Chat.Id;

See here for an example of class for json.

Jawad
  • 11,028
  • 3
  • 24
  • 37
0

So basically your issue is very simple. Your json variable is not an array, but object which contains ok and result. If I understand what you trying to do your foreach sentance should look like this: foreach (var item in json.result)

And whole code:

string link = "xxxxx";
var request = WebRequest.Create(link);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data);
string rs = reader.ReadToEnd();
dynamic json = JsonConvert.DeserializeObject(rs);
foreach (var item in json.result)
{
    MessageBox.Show(item.result.message.chat.id);
}

Also I suggest you to use specific types instead of dynamic. In cases like this strong types helps you to catch an issue before its even an issue. And to go extra mile I would suggest you to use HttpClient instead of WebRequest. More information: https://stackoverflow.com/a/53157401/9096935

Justinas
  • 26
  • 3