Json Array for C#string array

Asked

Viewed 310 times

2

I just need to get the names of the moderators and viewers of this json:

{
  "_links": {},
  "chatter_count": 16,
  "chatters": {
    "moderators": ["gumagames", "juliavidoto", "nightbot", "pinkpanthersz_", "victoriia66"],
    "staff": [],
    "admins": [],
    "global_mods": [],
    "viewers": ["andreschramm", "anotheruselessbot", "barbosza", "brancoxp", "froydz1515", "hurato2", "luiz522", "phoenixlabella", "rockmam", "sumarilion1988", "xoverxkill"]
  }
}

I am using JSON.Net but could not get a satisfactory result.

  • What have you tried?

  • Look if it’s that what you need.

  • @Brunno I was doing even so Brunno, was declaring the classes erroneously, even so thank you for the solution!

  • @Marcosbarbosa legal, posted as an answer.

2 answers

1

You need to create a class according to your Json, and after that just use:

JsonConvert.DeserializeObject<SeuObjeto>(stringJson);

That he will parse the Json into his class and return the instantiated object.

Follows the fiddle.

Generating Json classes in C#: json2csharp

0

Using the line command jq:

jq ".chatters.moderators,.chatters.viewers" ex.json

produces:

[
  "gumagames",
  "juliavidoto",
  "nightbot",
  "pinkpanthersz_",
  "victoriia66"
]
[
  "andreschramm",
  "anotheruselessbot",
  "barbosza",
  "brancoxp",
  "froydz1515",
  "hurato2",
  "luiz522",
  "phoenixlabella",
  "rockmam",
  "sumarilion1988",
  "xoverxkill"
]

or jq ".chatters.moderators+.chatters.viewers" to obtain a single array

Browser other questions tagged

You are not signed in. Login or sign up in order to post.