Reputation: 45
I'm working on a project where i want to extract some data from this link (http://www.eurogamer.net/ajax.php?action=json-comments&aid=1822104&start=0&limit=1000&filter=all&order=asc)
One part of this json file is here: [{"id":6044586,"u":"ubergine","uid":"472505","a":"2011\/community\/users\/4\/7\/2\/5\/0\/5\/user-472505-originalxoriginal-16-30-27.jpg","t":"3 weeks ago","k":"+16",**"p":"Prepare to Tri"**,"e":"","d":"","v":"unverified","gi":[],"s":"","mod":false,"total":"28"}]
I've tried Regex
but didn't find any good. Then I used Json.NET
using JArray
it parses the data and used JToken
but could not get. I'm using C# programming Language.
I want the specific value of p
key bold above?
Upvotes: 0
Views: 8715
Reputation: 45
I found the answer on another forum. It may help others. Here is the piece of code:
JArray parsedArray = JArray.Parse(jsonString);
foreach (JObject parsedObject in parsedArray.Children<JObject>())
{
foreach (JProperty parsedProperty in parsedObject.Properties())
{
string propertyName = parsedProperty.Name;
if (propertyName.Equals("p"))
{
string propertyValue = (string)parsedProperty.Value;
Console.WriteLine("Name: {0}, Value: {1}", propertyName, propertyValue);
}
}
}
And Here is the link for Complete code answer: https://social.msdn.microsoft.com/Forums/vstudio/en-US/df1d6c1b-349a-4f7f-9168-5bf01d320075/how-to-get-keyvalue-pair-from-json-jarray?forum=csharpgeneral
Upvotes: 2
Reputation: 11514
You may be better of making a class that maps to the json properties and using JsonConvert.DeserializeObject<>()
.
See this linqpad script: http://share.linqpad.net/7fpltw.linq
source here (note this is a LinqPad script so you will get errors if you just paste into visual studio):
void Main()
{
string thejson = @"[{
""id"": 6044586,
""u"": ""ubergine"",
""uid"": ""472505"",
""a"": ""2011\/community\/users\/4\/7\/2\/5\/0\/5\/user-472505-originalxoriginal-16-30-27.jpg"",
""t"": ""4 weeks ago"",
""k"": ""+16"",
""p"": ""Prepare to Tri"",
""e"": """",
""d"": """",
""v"": ""unverified"",
""gi"": [],
""s"": """",
""mod"": false,
""total"": ""28""
}, {
""id"": 6044596,
""u"": ""Fragtaster"",
""uid"": ""828120"",
""a"": ""2014\/community\/users\/8\/2\/8\/1\/2\/0\/user-828120-originalxoriginal-8-03-00.jpg"",
""t"": ""4 weeks ago"",
""k"": ""+44"",
""p"": ""This [Dark Souls 3] is one of those cases where <strong>\""more of the same\""<\/strong> is a bloody damn good-thing.<br \/>\n<br \/>\n<img src=\""https:\/\/tse2.mm.bing.net\/th?id=OIP.M607bf0698ed2368b9f9cc9e4ea244c77H0&pid=15.1\"" \/>"",
""e"": """",
""d"": """",
""v"": ""unverified"",
""gi"": [],
""s"": """",
""mod"": false,
""total"": ""66""
}]";
var theobj = JsonConvert.DeserializeObject<List<JsonData>>(thejson);
theobj.ForEach(o => o.p.Dump());
}
// Define other methods and classes here
public class JsonData
{
public string id { get; set; }
public string u { get; set; }
public string uid { get; set; }
public string a { get; set; }
public string t { get; set; }
public string k { get; set; }
public string p { get; set; }
public string e { get; set; }
public string d { get; set; }
public string v { get; set; }
public string[] gi { get; set; }
public string s { get; set; }
public string mod { get; set; }
public string total { get; set; }
}
Upvotes: 1