I am getting the JSON response from youtube API v3 for https://www.googleapis.com/youtube/v3/channels?part=statistics&id
and receiving JSON response as
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2zirKmd0WgUqY0RzlyD4hlACeWM\"";
items = (
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/u4TmQ5XfIQQg6y6u4Od2yULCVlc\"";
id = "HCcrj0EHvn_Y8";
kind = "youtube#channel";
statistics = {
commentCount = 0;
subscriberCount = 21694;
videoCount = 124582;
viewCount = 0;
};
},
{
etag = "\"rFqCJSkEICLP3Hq6a4AADI7kf48/2p4_mjrZLfhO6bDvH-RgAykNQr8\"";
id = UCX2v47KsDKqajrEYFV7GbBg;
kind = "youtube#channel";
statistics = {
commentCount = 6460;
subscriberCount = 494656;
videoCount = 33;
viewCount = 713607227;
};
}
}
when I am trying to display the
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"statistics.videoCount"] objectAtIndex:indexPath.row];
the value doesn't display on table view.
I tried to format the output as unsigned long ,int but the correct values don't show.
Please help
Thanks in advance
I think you need to add item to valueForKeyPath
cell.viewsChannel.text=[[_statistics valueForKeyPath:#"item.statistics.videoCount"] objectAtIndex:indexPath.row];
Related
I'm using an API to get weather condition and the retrieved dict is
dict = {
base = stations;
clouds = {
all = 92;
};
cod = 200;
coord = {
lat = "31.23";
lon = "121.47";
};
dt = 1476853699;
id = 1796231;
main = {
"grnd_level" = "1028.63";
humidity = 93;
pressure = "1028.63";
"sea_level" = "1029.5";
temp = "73.38";
"temp_max" = "73.38";
"temp_min" = "73.38";
};
name = "Shanghai Shi";
rain = {
3h = "0.665";
};
sys = {
country = CN;
message = "0.0125";
sunrise = 1476827992;
sunset = 1476868662;
};
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
wind = {
deg = "84.50239999999999";
speed = "5.97";
};
}
If I want the value of humidity, I just use
let humidityValue = dict["main"]["humidity"] and it works.
But the problem is I also want to get the value of description in weather
when I used let dscptValue = dict["weather"]["description"]
it retrieved nil.
How's that? and I notice there are two brackets around weather .I'm not sure whether it is the same with the statement without brackets.
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
}
);
How to get the value of description?
weather keys contains Array of Dictionary not directly Dictionary, so you need to access the first object of it.
if let weather = dict["weather"] as? [[String: AnyObject]], let weatherDict = weather.first {
let dscptValue = weatherDict["description"]
}
Note: I have used optional wrapping with if let for preventing crash with forced wrapping.
Weather is an array of dictionaries.
dict["weather"][0]["description"]
may give you the expected result.
I have an application which retrieves a JSON file. Here you are a piece of my code:
Array definition
var photos: NSArray = []
How I populate the array:
ezJson().createRequest("http://myapiurl/load", type: "GET", params: nil, completion: {(returnedObject : AnyObject?, error : NSError?)in
if returnedObject{
self.photos = returnedObject as NSArray
self.tableView.reloadData()
}
})
println(self.photos)
{
created = {
date = "2014-06-13 18:35:46";
timezone = "Europe/Madrid";
"timezone_type" = 3;
};
description = description1;
id = 3;
name = 539b286277617;
},
{
created = {
date = "2014-06-13 18:38:38";
timezone = "Europe/Madrid";
"timezone_type" = 3;
};
description = description2;
id = 4;
name = 539b290ed8577;
}
println(self.photos[0])
{
created = {
date = "2014-06-13 18:35:46";
timezone = "Europe/Madrid";
"timezone_type" = 3;
};
description = description1;
id = 3;
name = 539b286277617;
}
The problem is, I don't know how to get a particular item. I've tried:
println(self.photos[0]) // it works
println(self.photos[0]["name"] // Xcode crash "Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254"
println(self.photos[0].name) // returns nil
How can I access to the name parameter ?
It seems that you are casting a String to NSArray. This won't give you the effect that you want.
First of all, if you want to acces your elements by name, you want an NSDictionary.
Then it propably still won't work, as there is no implicit conversion between the types so you will have to either parse it yourself or use some JSON library.
Last : your JSON is incorrect.
I have a dictionary like below:
{
76 = (
{
language = en;
optionid = 1;
response = ffgh;
}
);
74 = (
{
language = en;
optionid = 1;
response = "Herbert S.B. Baraf, MD";
}
);
75 = (
{
language = en;
optionid = 1;
response = ffgh;
}
);
73 = (
{
language = en;
optionid = 1;
response = Excellent;
}
);
}
I am not getting the key value array from my dictionary using below code:
NSMutableArray *Array=[m_MutDictAnswers objectForKey:m_strRuleQuestion ];
While logging the Array is empty and the m_strRuleQuestion is 73.
I don't know why I am getting an empty array.
In my json string m_strRuleQuestion is "73" , but when I edit the json to m_strRuleQuestion to 73, I am getting the correct array. I need to fix this issue with out editing the json string. Can any one help me.
NSMutableArray *Array=[m_MutDictAnswers objectForKey#"73"]
try this and then check what are you getting.
I thing this dictionary is in array first you get array may its index of 0 after that fetch dictionary array
NSString *string=[[[myarray objectAtIndex:0]objectForKey:#"72"]objectForKey:#"language"];
I'm making the following simple request to Facebook. My goal is to grab the names of all a user's photo albums:
[[AppDelegate sharedDelegate].facebook requestWithGraphPath:#"me/albums" andDelegate:[AppDelegate sharedDelegate]];
Then, I get a JSON response from the server like such:
data = (
{
"can_upload" = 1;
count = 4;
"cover_photo" = 321491374598618;
"created_time" = "2012-06-04T21:46:23+0000";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 321491371265285;
link = "http://www.facebook.com/album.php?fbid=321491371265285&id=100002132746588&aid=73680";
name = "Photos";
privacy = custom;
type = normal;
"updated_time" = "2012-06-04T22:08:39+0000";
},
{
"can_upload" = 0;
count = 1;
"cover_photo" = 318401854907570;
"created_time" = "2012-05-31T00:00:35+0000";
description = "Which Friend Stalks You the Most?";
from = {
id = 100002132746588;
name = "Owner....";
};
id = 318401848240904;
link = "http://www.facebook.com/album.php?fbid=318401848240904&id=100002132746588&aid=73163";
name = "Which Friend Stalks You the Most?";
privacy = friends;
type = normal;
"updated_time" = "2012-05-31T00:00:36+0000";
},
And so on. Problem is, when I try to parse the data with:
NSLog(#"%#", [result objectForKey:#"name"]);
I get null. I assume this is happening because the NSDictionary that the data is returned in does not know which name entry to concentrate on. How do I parse the data so that I get the names of all the albums?
"result" is actually an array of dictionaries, so loop through the array and then grab name from each element in the array.
for (NSDictionary *anAlbum in [result objectForKey:#"data"]) {
NSLog(#"%#", [anAlbum objectForKey:#"name"]);
}
I'm sorry in advance for a newbie question. I've been working on this for days, and I'm having comprehension problems. I used sudzc to help connect to my web service. The example function only returns 1 result (the last one). How can I load all the results into an array so that I can find CSHR_NUM = 8? I'm not sure if there is a problem with:
- (void) GetCashiersHandler: (id) value
Any help would be greatly appreciated!
The sudzc logging returns (there are hundreds of entries, I cut it down):
<TC diffgr:id="TC1" msdata:rowOrder="0">
<CSHR_POS_NAME>JACKSON<CSHR_POS_NAME />
<CSHR_NUM>8</CSHR_NUM>
</TC>
<TC diffgr:id="TC2" msdata:rowOrder="1">
<CSHR_POS_NAME>ALISON</CSHR_POS_NAME>
<CSHR_NUM>464</CSHR_NUM>
</TC>
I use the example method of calling:
[service GetCashiers:self action:#selector(GetCashiersHandler];
// Handle the response from GetCashiers to get login in name.
- (void) GetCashiersHandler: (id) value
{
// Do something with the CXMLNode* result
CXMLNode *result = (CXMLNode*)value;
NSLog(#"I returned the value: %#", result);
}
NSLog Results:
I returned the value: {
GetCashiersResult = {
diffgram = {
NewDataSet = {
TC = {
"CSHR_NUM" = 464;
"CSHR_POS_NAME" = ALLISON;
};
};
};
schema = {
element = {
complexType = {
choice = {
element = {
complexType = {
sequence = {
element = 0;
};
};
};
};
};
};
};
};
}
NSMutableArray * myData = [[NSMutableArray alloc] initWithCapacity:5];
// loop over items to add and add them with
[myData addObject:myItem];