Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Park
5,719 PointsHow would I parse this set of JSON Data?
Hi everyone, I am having trouble parsing this set of JSON formatted data: { "sportsNews": [ { "title": { "text": "Ryan Tannehill's 2 late TDs rally Dolphins to rivalry win over Bills", "href": "http://scores.espn.go.com/nfl/recap?gameId=400554338" }, "description": { "text": "win over Bills (November 14, 2014, 12:08 AM ET)", "href": "http://scores.espn.go.com/nfl/recap?gameId=400554338" } } in order to access keys would I do a valueForKeyPath: @"title.text"?
3 Answers
Jason Wayne
11,688 PointsAssuming that you already got the sportsNews array, and assuming there is more to one of that set of information, you could enumerate through all of them and get the values out.
For example,
for (NSDictionary *theDictionary in sportsNewArray) { NSString * theTitleOfThePost = [theDictionary valueForKeyPath:@"title.text"]; }
Michael Park
5,719 Pointsassuming that there is more of that set of data and I have the sportsNews array would this code work? I have a table of blank cells. I am accessing results first (which is so in my data), then the sportsNews collection.
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSArray *sportsArray = [dataDictionary valueForKeyPath:@"results.sportsNews"];
for (NSDictionary *sportsDictionary in sportsArray) {
sportPost *sp = [sportsPost sportPostWithTitle:[sportPostDictionary valueForKeyPath:@"title.text"]];
sp.description = [sportPostDictionary valueForKeyPath:@"description.text"];
[self.sportPost addObject:sp];
Michael Park
5,719 Points{ results: { "sportsNews": [ { "title": { "text": "Dallas-area teacher fired over racial tweets", "href": "http://news.yahoo.com/dallas-area-teacher-fired-over-ferguson-tweets-154012279.html" }, "description": "DUNCANVILLE, Texas (AP) — A Dallas-area school board fired a teacher Friday who posted racially charged tweets about the fatal shooting of a black 18-year-old by a white police officer in Ferguson, Missouri." }, { "title": { "text": "Blue Cash Preferred® Card", "href": "https://beap.gemini.yahoo.com/mbclk?bv=1.0.0&es=lcRLXhYGIS.y7NlYXOJwFZTrCpKhY4O0DRWjGxw0nV9yjO6uLe297HeqCaviy_Z81hc8nzjJIyHZhmJdHVI4nk_cDdq3F_kty9i8tzXuxUHq0GeWJmui_DekqRE80Bd0QS7Fg0g9v8Nx.4YIkVks0Qv14PkPPyDZaK4bgOcta1..bzM.SN.PLj7xEsVzwNKBBeNMCEK0w4TLUq5cSf7wHw5mWu1UhNYw2ADGkpQMnK5VpG_1mPFMTlv_tMVk._GZT0PlWkh2ChHuWMRtyBuXIZRYnMyQifBJo6TC3zi2Mx8cbPEXBfjAohdZAcqN5PbGd4.WneUnF246XITTLU6K2JuhJnDgLHo4KMwvjRLayaUARlOFdG3.ghRjg1ocCD4vWXeNiweDTvZATw_0oBhVE1TCqregKl78MWewmyPFiwZ2irocfjRF_KhCu0MFkzbh6NlORADkklBLAZEtFrn927M9_GYdxNSjMURYRM3Yb8Bc6ivbUZ4VtyWFCBM4X4o3Gtaplb2pL4ammNqTrBvgLI_VUncY46ZqLOr6NVsG1hD4JCLpJq_ltqptq6O4mCPSBJIGELb1oTVjopi7ax2.F1mNZmGOAOcbaeqUS00FlC8f4ZJBnBehc0_r6exAePDrMq.vpLzPAn8JsbTSazID0YoKcDFimhDry5E5DugIGzzHb2.4Elz766sVUjLrhcRObcMFxIkCDp64wH7vcjKo5Hm9odG1nZ.VWJKNhr9EQ.tql6SXk4KwjaVjVphmet3h1w--%26lp=" }, "description": "Earn 6% Cash Back at US Supermarkets. Learn More!" }, { "title": { "text": "", "href": "" }, "description": "There’s no better time of year for the video game industry than the holidays. Most of the biggest releases hit store shelves in November while all the games from years past go on sale. It’s a great time to be a gamer, which is why we thought it would be appropriate to share some of GameStop’s best…" },