I'm struggling with this dataset:
stopsArray: (
(
{
DistanceFromStart = 0;
Ordinal = 1;
Stop = {
Lat = "38.90282440185547";
Lon = "-77.03208160400391";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 1001259;
StopId = "DcCi_1445_7727";
StopName = "NW 14TH ST & NW K ST";
};
},
{
DistanceFromStart = "0.443314063224555";
Ordinal = 2;
Stop = {
Lat = "38.90834426879883";
Lon = "-77.03208923339844";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 1001393;
StopId = "DcCi_1445_7808";
StopName = "NW 14TH ST & NW RHODE ISLAND AV";
};
},
{
DistanceFromStart = "1.050716048463951";
Ordinal = 3;
Stop = {
Lat = "38.91703033447266";
Lon = "-77.03196716308594";
OrgAbbr = "<null>";
OrgId = DcCi;
StopCode = 17348;
StopId = "DcCi_1445_6674";
StopName = "NW 14TH ST & NW U ST";
};
},
I'm not sure if this is an array, a dictionary, or a combination. How do I tell the difference?
How would I pull out the "Ordinal" field, as well as elements in the "Stop" dataset?
To get the stopsArray data, I used this line:
stopsArray = [variantArray valueForKey:#"Stops"];
I've tried to pull the first Ordinal field (i.e. 1) with this:
NSMutableArray *array = stopsArray[0];
NSString *string = [array valueForKey:#"Ordinal"];
NSLog(#"string: %#", string);
But that results in:
string: (
1,
2,
3,
4,
5,
6,
7
)
Any ideas to stop my hair coming out would be most welcome.
I'll keep at it meanwhile.
Firstly, its an array of objects of type dictionary.
Secondly, the data doesn't seem to be properly formatted.
Its like an array with a single object of type dictionary, but that single object contains all the different dictionary values.
hence your result of
string: (
1,
2,
3,
4,
5,
6,
7
)
What seems to me the problem is the second open bracket
stopsArray: (
(
{
Try doing this...
NSMutableArray *array = stopsArray[0];
NSMutableArray *actualDataArray = array[0];
and then
NSString *string = [actualDataArray[INDEX_OF_DIC_OBJECT] valueForKey:#"Ordinal"];
NSLog(#"string: %#", string);
Lemme know if it works.
Updated Solution
What eventually worked for David DelMonte.
NSString *string = [[[stopsArray objectAtIndex:0] objectAtIndex:0] objectForKey:#"Ordinal"];
Related
data = (
{
date = "2016-01-20";
"end_time" = "11:10:00";
"function_code" = RCV;
"operator_id" = JOHN;
"start_time" = "11:00:00";
"total_time" = 10;
"total_units" = 19;
},
{
date = "2016-01-20";
"end_time" = "12:25:00";
"function_code" = PIK;
"operator_id" = JOHN;
"start_time" = "12:15:00";
"total_time" = 10;
"total_units" = 26;
}
)
this array containing 2 dictionary ,i have to get the endtime from the first dictionary and starttime from the second dictionary and i want to calculate the break time from this value.i know how to get the value dictionary . data[0][#"end_time"] and data[1][#"start_time"] this is sufficient if array contains two elements .but if array has more than 5 means,i want to iterate the array .i will update my code what i did ...
my array name is arrData
for (int i 0; i<arrData.count; i++) {
dictData =arrData[i];
NSString *startTime =[dictData objectForKey:#"start_time"];
NSString *totalTime = [dictData objectForKey:#"total_time"];
NSNumber *numTotalUnits =[dictData objectForKey:#"total_units"];
NSString *functionCode = [dictData objectForKey:#"function_code"];
NSString *endTime = [dictData objectForKey:#"end_time"];
NSLog(#"%#",endTime);
[array1 addObject:endTime];
if (arrData[i+1] > arrData.count) {
dictData = arrData[i+1];
NSLog(#"%#",dictData);
NSString *strStartTime = [dictData objectForKey:#"start_time"];
NSLog(#"%#",strStartTime);
[array1 addObject:strStartTime];
NSLog(#"%#",array1);
}
}
i tried this coding but i got error like index 2 beyond bounds[0..1]
Try for (int i = 0; i < arrData.count - 1; i++) {.
In your current code, you let i become equal to arrData.count - 1 in the loop, which means that when you check i + 1 you're looking past the final array element.
In My Application i am Using ArcGIS . in that one i want Search one place and get the places related to that name. Now I want to Parse all the address of that places and put them in a table View. after typing the address and after i press search Button i will get all the Markers on the Map. Now i'm trying to get the complete address of the Place But i was unable to do this.I want to get "place_addr" value from the below string can any one please help me how can i get that value in ios
AGSFindLocationResult name=Disneyland, extent=AGSEnvelope: xmin = -13129988.221853, ymin = 4000701.846518, xmax = -13124422.247314, ymax = 4007401.106877, spatial reference: [AGSSpatialReference: wkid = 102100, wkt = null], graphic=geometry: AGSPoint: x = -13127204.667522, y = 4004050.640580, spatial reference: [AGSSpatialReference: wkid = 102100, wkt = null], symbol: { AGSPictureMarkerSymbol: imageName: BluePushpin.png, width: 36.000000, height: 36.000000 }, attributes: {
AddBldg = "";
AddNum = "";
AddNumFrom = "";
AddNumTo = "";
"Addr_type" = POI;
City = Anaheim;
Country = USA;
DisplayX = "-117.923687";
DisplayY = "33.815467";
Distance = 0;
LangCode = "";
"Loc_name" = "Gaz.WorldGazetteer.POI2";
"Match_addr" = Disneyland;
Nbrhd = "";
Phone = "(714)781-4565";
PlaceName = Disneyland;
"Place_addr" = "1313 S Disneyland Dr Anaheim, CA 92802";
Postal = "";
PostalExt = "";
Rank = "2.5";
Region = California;
Score = 100;
Side = "";
StAddr = "";
StDir = "";
StName = "";
StPreDir = "";
StPreType = "";
StType = "";
Subregion = Orange;
Type = "Amusement Park";
URL = "";
X = "-117.923687";
Xmax = "-117.898691";
Xmin = "-117.948691";
Y = "33.815467";
Ymax = "33.84047000000002";
Ymin = "33.79047";
}, visible: 1
IF what you're getting back really is a string, then you should look at the string searching methods like rangeOfString.
That will return an NSRange that contains your label, "place_addr".
Then you'd want to search from that point forward for a semicolon, using the related method rangeOfString:options:range:, that searches in a specific range of a string.
Finally, you'd need to calculate a range for the contents, that would be 5 characters past the end of the NSRange of "place_addr", and up to, but not including, the final semicolon.
Then you'd use substringWithRange to extract the string who's range you calculated above.
Give that a try, and come back if you have trouble.
Please try the code below
- (NSArray *)findKey:(NSString *)key inString:(NSString *)allString
{
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *splitArray = [allString componentsSeparatedByString:#";"];
for (NSString *s in splitArray) {
NSArray * keyValueArry = [s componentsSeparatedByString:#"="];
if([keyValueArry count] > 1)
{
NSString *_key = [keyValueArry objectAtIndex:0];
NSString *_value = [keyValueArry objectAtIndex:1];
if([_key isEqualToString:key])
{
[result addObject:_value];
}
}
}
return result;
}
where key is place_addr, and allString is as your sample string.
I have created an NSDictionary named "myData"
which contains the following JSON response:
{
listInfo = (
{
date = 1392157366000;
dateAsString = "02/11/2014 22:22:46";
id = 6;
address = 542160e0000c;
myLevel = 13;
},
{
date = 1392155568000;
dateAsString = "02/11/2014 21:52:48";
id = 5;
address = 542160e0000c;
myLevel = 13;
}
);
}
I need to retrieve each of the [dateAsString] key/value pairs.
I've tried: NSString *dateAsString=[[myData valueForKeyPath:#"dateAsString"][0] objectForKey:#"myData"]; without any luck.
Any suggestions are greatly appreciated.
I think this will work:
NSArray* dateStringArray = [listInfo valueForKeyPath:#"#unionOfObjects.dateAsString"]
I believe it will give you an array of strings. If you need to stuff that back in a dictionary, that should be fairly easy.
It's not clear what your "myData" looks like... so I used the listInfo array of dicts shown.
My application has an NSDictionary containing many other NSDictionary inside it. If I print out this dictionary it reads as follows:
oxip = {
created = "2014-02-10 14:42:59";
lastMsgId = "";
requestTime = "1.6434";
response = {
code = 001;
debug = "";
message = success;
request = getHierarchyByMarketType;
text = "\n";
williamhill = {
class = {
id = 1;
maxRepDate = "2014-02-10";
maxRepTime = "07:31:48";
name = "UK Football";
text = "\n";
type = (
{
id = 2;
lastUpdateDate = "2013-12-26";
lastUpdateTime = "13:32:54";
market = (
{
betTillDate = "2014-02-15";
betTillTime = "15:00:00";
date = "2014-02-15";
id = 140780553;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South v Dundee - Match Betting";
participant = (
{
handicap = "";
id = 496658381;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Dundee;
odds = "11/8";
oddsDecimal = "2.38";
text = "\n\n\n\n\n\n";
},
{
handicap = "";
id = 496658380;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Draw;
odds = "5/2";
oddsDecimal = "3.50";
text = "\n";
},
{
handicap = "";
id = 496658379;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South";
odds = "11/8";
oddsDecimal = "2.38";
text = "\n";
}
);
text = "\n";
time = "15:00:00";
}
What is the best possible way for my application to reach the NSDictionary with the name of:
name = "Queen of the South v Dundee - Match Betting"
without the need of going through each individual dictionary and finding its object for key?
You can use valueForKeyPath for that. It accepts a path, separated by dots. Example:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://dl.dropboxusercontent.com/u/1365846/21680479.json"]]
options:0
error:nil];
NSLog(#"%#", [dict valueForKeyPath:#"response.williamhill.class.type.market.name"]);
This depends on the representation of dictionary. If the williamhill part is changing, then it does not work, of course.
There is no way to obtain a reference within a map data type (in this case, an NSDictionary) without traversing it. Think of the simplified version of this problem: You have a linked list with N elements and you wish to reach the N'th element. Because this is a linked list, you'll have to go through all other N-1 nodes in order to obtain the last reference.
An NSDictionary is a hash based data type in which keys and values are stored. In the case you describe, you have no reference to the nested object (an NSDictionary itself) so you must also traverse all of the dictionaries containing it.
Hope this helps point you in the right direction.
I receive and array of dictionaries from a JSON feed and assign it to an NSMutableArray called jsonArray i.e:
jsonArray = [deserializedData objectForKey:#"reports"];
The feed looks like this:
reports = (
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 1;
"eng_name" = "Alex McPherson";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 59;
lat = "51.1438330";
live = 1;
lng = "0.8693330";
location = "17 Victoria Crescent, Ashford, TN23 7HL";
name = "Alex McPherson";
phone = 01233000000;
rid = "A5C963-C95B-C3D639";
title = "#A5C963-C95B-C3D639, Litter";
tm = "2013-04-28 20:44:20";
type = 5;
"type-text" = "Litter";
},
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 2;
"eng_name" = "Rob Burt";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 122;
lat = "51.1415000";
live = 1;
lng = "0.8715000";
location = "38 Beaver Road, Ashford, TN23 7RP";
name = Alex;
phone = 01233000000;
rid = "5A5C96-9072-6BAFA9";
title = "#5A5C96-9072-6BAFA9, Litter";
tm = "2013-04-28 20:35:56";
type = 8;
"type-text" = "Litter";
};
what I would like to do is insert a new value for key lets say into this jsonArray: distance = "0.16km" but my brain is just not working tonight....
so the new jsonArray should looks like this with the added key value mentioned above:
reports = (
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 1;
"eng_name" = "Alex McPherson";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 59;
lat = "51.1438330";
live = 1;
lng = "0.8693330";
distance = "0.16km";
location = "17 Victoria Crescent, Ashford, TN23 7HL";
name = "Alex McPherson";
phone = 01233000000;
rid = "A5C963-C95B-C3D639";
title = "#A5C963-C95B-C3D639, Litter";
tm = "2013-04-28 20:44:20";
type = 5;
"type-text" = "Litter";
},
{
address = "The street";
email = "alex#blah.co.uk";
"eng_id" = 2;
"eng_name" = "Rob Burt";
"eng_thumb" = "http://someurl/image/1.png";
form = Test;
id = 122;
lat = "51.1415000";
live = 1;
lng = "0.8715000";
distance = "2.13km";
location = "38 Beaver Road, Ashford, TN23 7RP";
name = Alex;
phone = 01233000000;
rid = "5A5C96-9072-6BAFA9";
title = "#5A5C96-9072-6BAFA9, Litter";
tm = "2013-04-28 20:35:56";
type = 8;
"type-text" = "Litter";
};
basically I have a calculation that takes the long and lat from the feed and works out how far the poi is from my current location of which then I sort the array by using a sort descriptor based on the distance key that I want to insert above. I have the code for this just stuck on inserting into the existing nsmutablearray above
Read the deserialized JSON array as an NSArray (not NSMutableArray).
Then create a mutable copy of that array using something like:
NSMutableArray *mutableArray = [originalArray mutableCopy];
Then insert items into mutableArray.