How do I setup a NSDictionary this this JSON?

3042
3
06-14-2010 01:01 PM
ChanceYohman
New Contributor
{
"spatialReference": {"wkid" : 4326},
"candidates" : [
  {
  "address" : "1 MASON ST",
  "location" : { "x" : -122.408951, "y" : 37.783206 },

  "score" : 75,
  "attributes" : {"StreetName" : "MASON", "StreetType" : "ST"}
  },
  {
  "address" : "49 MASON ST",
  "location" : { "x" : -122.408986, "y" : 37.783460 },

  "score" : 27,
  "attributes" : {"StreetName" : "MASON", "StreetType" : "ST"}
  } 
]

}

Is it a NSDictionary of NSDictionaries?
0 Kudos
3 Replies
ChanceYohman
New Contributor
This seems to work, but it's not setting up the spatial reference correctly:

NSDictionary *sRJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"4326", @"wkid", nil];
    NSDictionary *locationJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"-122.408951", @"x", @"37.783206", @"y", nil];
    NSDictionary *attributesJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"MASON", @"StreetName", @"ST", @"StreetType", nil];
    NSDictionary *json=[NSDictionary dictionaryWithObjectsAndKeys:sRJSON, @"spatialReference", @"1 MASON ST", @"address", locationJSON, @"location", @"75", @"score", attributesJSON, @"attributes", nil];
    AGSAddressCandidate *candidate=[[AGSAddressCandidate alloc ]initWithJSON:json];
   
    NSLog(@"***Address Candidate Start***");
    //Display the address cey 03.18.10
    NSLog(@"Address Candidate String: %@",[candidate addressString]);
   
    //Display the location cey 03.18.10
    //What's the spatial reference of the locator? How can I find that out at the endpoint? Wouldn't it make sense to be able to retrieve the spatial reference from the point?
    AGSPoint *location = [candidate location];
    NSLog(@"Address Candidate @ (%f,%f,%@)",[location x], [location y], [location spatialReference]);
   
    NSDictionary *address = [candidate address];
    NSLog(@"Address Candidate Fields Count: %d", [address count]);
    for(NSString *aKey in address)
    {
        NSLog(@"Key: %@ Value: %@", aKey, [address objectForKey:aKey]);
    }
   
    NSDictionary *attributes = [candidate attributes];
    NSLog(@"Address Candidate Attributes Count: %d", [attributes count]);
    for(NSString *aKey in attributes)
    {
        NSLog(@"Key: %@ Value: %@", aKey, [attributes objectForKey:aKey]);
    }
   
    NSLog(@"Address Candidate Score: %f",[candidate score]);
    NSLog(@"***Address Candidate End***");


Output:

2010-06-14 17:20:07.679 AGSAddressCandidate[973:207] ***Address Candidate Start***
2010-06-14 17:20:07.682 AGSAddressCandidate[973:207] Address Candidate String: 1 MASON ST
2010-06-14 17:20:07.683 AGSAddressCandidate[973:207] Address Candidate @ (-122.408951,37.783206,(null))
2010-06-14 17:20:07.683 AGSAddressCandidate[973:207] Address Candidate Fields Count: 0
2010-06-14 17:20:07.684 AGSAddressCandidate[973:207] Address Candidate Attributes Count: 2
2010-06-14 17:20:07.684 AGSAddressCandidate[973:207] Key: StreetType Value: ST
2010-06-14 17:20:07.685 AGSAddressCandidate[973:207] Key: StreetName Value: MASON
2010-06-14 17:20:07.685 AGSAddressCandidate[973:207] Address Candidate Score: 75.000000
2010-06-14 17:20:07.686 AGSAddressCandidate[973:207] ***Address Candidate End***
0 Kudos
NimeshJarecha
Esri Regular Contributor
The spatialReference parameter in your JSON is outSR parameter and not the address candidates spatial reference value.

===For REST help doc===
outSR - The well-known ID of the spatial reference or a spatial reference json object for the returned address candidates. For a list of valid WKID values, see Projected coordinate Systems and Geographic coordinate Systems.
This parameter was added at 10.
===For REST help doc===

Change your code lines as following and you'll see location's spatial reference in the output.

//NSDictionary *sRJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"4326", @"wkid", nil];
    //NSDictionary *locationJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"-122.408951", @"x", @"37.783206", @"y", nil];
NSString *locationJSON = @"{\"x\":-122.408951,\"y\":37.783206,\"spatialReference\":{\"wkid\":4326}}";
    NSDictionary *attributesJSON=[NSDictionary dictionaryWithObjectsAndKeys:@"MASON", @"StreetName", @"ST", @"StreetType", nil];
    NSDictionary *json=[NSDictionary dictionaryWithObjectsAndKeys:@"1 MASON ST", @"address", [locationJSON JSONValue], @"location", @"75", @"score", attributesJSON, @"attributes", nil];
    AGSAddressCandidate *candidateTest =[[AGSAddressCandidate alloc ]initWithJSON:json];

Hope this helps! Please let me know if you have any further queries.

Regards,
Nimesh
0 Kudos
ChanceYohman
New Contributor
Thanks Nimesh.
0 Kudos