Answer the question
In order to leave comments, you need to log in
Can't deal with Json in c#?
How do I copy the title and url into string variables. I tried many ways, but nothing works. It is necessary either by standard means or with Newtonsoft .Json;
Preferably a way where there is less code. Thank you.
{
"response":{
"count":198,
"items":[
{
"id":340125039,
"owner_id":34254853,
"artist":"IOWA",
"title":"Маршрутка",
"duration":190,
"url":"http:\/\/cs7-5v4.vk-cdn.net\/p12\/f2b760cb436de0.mp3?extra=e4YwZ6p7cW89g9sxd_umf32-REUp46lEPjiCU-PerT5_VH7v6wPJxEnRZjf2I0nY_yJ8FQs7x_hyXH0dGtr27cP32ekSlx_F",
"genre_id":9
}
]
}
}
Answer the question
In order to leave comments, you need to log in
1) Using Newtonsoft .Json, you translate json into an xml document, for example, XDocument
2) Then from xml, using XPath, you get to the desired element
. Suppose that your json is in txt1.txt
var str1 = File.ReadAllText(@"Data\txt1.txt");
var xdoc = JsonConvert.DeserializeXNode(str1);
var title = xdoc.XPathSelectElements("response/items/title").Select(x => x.Value).FirstOrDefault();
var url = xdoc.XPathSelectElements("response/items/url").Select(x => x.Value).FirstOrDefault();
Maybe a little late, but the option with converting to XML is terrible. If there is no need / desire / opportunity to use the model, then there are the following options:
1. LinqToJson . Released in Newtonsoft Json.NET
2. JsonPath . Implemented there.
Regex is a fairly standard facility? If not, you can use String.Split() with quotes.
But in general, I like Pavel Osadchiy 's version the most (its second part):
In general, it is better to create a class into which json can be deserialized. And then we deserialize into it using Newtonsoft .Json
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question