Answer the question
In order to leave comments, you need to log in
How to pull data in json format using newtonsoft?
task: pull out the number (count) of a certain service (name) into a variable
, for example, I need to get (count), which has name == facebook
, this is what I get into the variable
string item = "[{"id":"2","name":"Vkontakte","count":"108","logo":"/uploads/logo/vk.png","cost":" 15"},{"id":"3","name":"Odnoklassniki","count":"6","logo":"/uploads/logo/ok.png","cost":"6 "},{"id":"4","name":"Avito","count":"127","logo":"/uploads/logo/av.png","cost":"5" },{"id":"5","name":"viber","count":"127","logo":"/uploads/logo/vi.png","cost":"3"} ,{"id":"6","name":"telegram","count":"126","logo":"/uploads/logo/te.png","cost":"3"},{"id":"7","name":"whatsapp","count":"126"," logo":"/uploads/logo/wh.png","cost":"1"},{"id":"8","name":"WeChat","count":"126","logo ":"/uploads/logo/wc.png","cost":"6"},{"id":"9","name":"Google,Youtube,Gmail","count":"124" ,"logo":"/uploads/logo/go.png","cost":"5"},{"id":"10","name":"youla.io","count":"126 ","logo":"/uploads/logo/yu.png","cost":"2"},{"id":"11","name":"Yahoo","count":"125","logo":"/uploads/logo/yo.png","cost":"2"},{"id":"12"," name":"Microsoft","count":"125","logo":"/uploads/logo/mi.png","cost":"1"},{"id":"13","name ":"facebook","count":"125","logo":"/uploads/logo/fb.png","cost":"1"},{"id":"14","name" :"Rambler.ru","count":"126","logo":"/uploads/logo/ra.png","cost":"1"},{"id":"15","name ":"yandex","count":"123","logo":"/uploads/logo/ya.png","cost":"5"},{"id":"16","name":"instagram","count":"125","logo":"/uploads/logo/in.png"," cost":"4"},{"id":"17","name":"drugvokrug.ru","count":"124","logo":"/uploads/logo/we0.png", "cost":"1"},{"id":"18","name":"mamba.ru","count":"125","logo":"/uploads/logo/78.jpg" ,"cost":"1"},{"id":"19","name":"Qiwi","count":"125","logo":"/uploads/logo/qi.png", "cost":"3"},{"id":"20","name":"WebMoney","count":"126","logo":"/uploads/logo/webmoney.png","cost":"5"},{"id":"21","name":"fotostrana.ru", "count":"126","logo":"/uploads/logo/fotostrana.png","cost":"1"},{"id":"22","name":"ICQ"," count":"126","logo":"/uploads/logo/ic.png","cost":"1"},{"id":"23","name":"Twitter","count ":"127","logo":"/uploads/logo/tw.png","cost":"2"},{"id":"24","name":"Mail.ru"," count":"126","logo":"/uploads/logo/ma.png","cost":"1"},{"id":"25","name":"Uber","count":"126","logo":"/uploads/logo/ub.png","cost":"3"} ]";
then I create an object for parsing json (newtonsoft library)
JObject test = JObject.Parse(item);
how do I get the count whose name is "name":"facebook"
string countFacebook = (string)test. ?????????????????????????????????????????????
thank you in advance )
Answer the question
In order to leave comments, you need to log in
There is a search: How to correctly describe a class for the Newtonsoft.Json C# library?
Something like:
var item = "ваш json что показали";
var json = JToken.Parse(item);
var count = json[11]["count"].ToObject<string>();
var array = JArray.Parse(item);
var count = array.Where(x => x["name"].ToObject<string>() == "facebook").First()["count"].ToObject<string>();
var array = JArray.Parse(item);
foreach (var element in array)
{
if (element["name"].ToObject<string>() == "facebook")
{
count = element["count"].ToObject<string>();
}
}
Your json is an array, which means you need to use JArray instead of JObject .
Further, if you are sure that the value name for which you want to know the value of count exists and only use Single . To retrieve a value of a specific type, there is a Value method
var test = JArray.Parse(json);
var count = // count = 125
test
.Single(j => j.Value<string>("name") == "facebook")
.Value<int>("count");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question