DynamoDB - how to extract NSString from Data set - ios

Using the DynamoDBItemRequest in extracting the 'firstName' object, what I get is this:
{S: Will,N: (null),B: (null),SS: (
),NS: (
),BS: (
),}
I've tried extracting the first object but it keeps giving me an error. After a bit of digging I realised that DynamoDB does not return an NSString or an NSArray object. Anyone have any luck extracting the dataset?
Here's my code -
-(void)tap
{
DynamoDBGetItemRequest *getItemRequest = [[DynamoDBGetItemRequest new] autorelease];
DynamoDBAttributeValue *attributeValue = [[[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:#"%d", 1]] autorelease];
getItemRequest.tableName = TEST_TABLE_NAME;
getItemRequest.key = [NSMutableDictionary dictionaryWithObject:attributeValue forKey:TEST_TABLE_HASH_KEY];
DynamoDBGetItemResponse *getItemResponse = [[AmazonClientManager ddb] getItem:getItemRequest];
NSMutableDictionary *userPreferences = getItemResponse.item;
NSArray *abc = [userPreferences objectForKey:#"lastName"];
NSLog(#"%#", abc);
}
Here's my attempt at the code that keeps giving me error->
// NSString *abb = abc[1];
// NSLog(#"%#", abb);

In DynamoDB, an attribute can be of only one type (String, Number, Binary, StringSet, NumberSet, BinarySet). The object that you are printing out is of type "String", and has the content "Will". Dynamo would probably be clearer if it printed it like this:
{S:Will}
But instead it's just converting the AttributeValue to a String, and showing you all of the other (empty) values inside of it:
{S: Will,N: (null),B: (null),SS: ( ),NS: ( ),BS: ( ),}
Everything else is just noise; all that's important is that it's a String with the content "Will".

Related

Concatenating values to the same key in an NSMutableDictionary

I am getting data from my database and the data is being retrieved using a while loop.
success = [db executeQuery:#"SELECT * FROM apidataTwo;"];
while([success next]){
int first = [success intForColumn:#"id"];
NSString *id = [NSString stringWithFormat:#"%d",first];
[_tempArray addObject:id];
NSString *country_name = [success stringForColumn:#"country_name"];
[_tempArray addObject:country_name];
NSString *breezometer_description = [success stringForColumn:#"breezometer_description"];
[_tempArray addObject:breezometer_description];
NSString *country_description = [success stringForColumn:#"country_description"];
[_tempArray addObject:country_description];
NSString *dateString= [success stringForColumn:#"dateString"];
[_dateSectionArray addObject:dateString];
[_dataDictionary setObject:_tempArray forKey:dateString];
}
Suppose we get the same key in different iterations of the loop. When I pass the array to the NSMutableDictionary, the previous values will be replaced and lost.
And if I keep updating the NSMutableArray, then the values of a previous key will also be added to a different key.
So in situations like this when we want to concatenate the values to the same key, then what should be our approach.
The dictionary should look like this:
{
2016-10-05" = (
5,
"United States",
"Fair Air Quality",
"Good air quality"
);
"2016-10-06" = (
5,
"United States",
"Fair Air Quality",
"Good air quality"
);
}
Once you have figured out the key for this batch of data, try to retrieve an object from the dictionary for that key. If objectForKey: returns nil, then create a new mutable array. Then set that array as the dictionary's object for that key.
Every new batch of data is then added to the array, not to the dictionary. Here's a sketch of the structure:
while( /* processing data */){
// Collect this batch
NSArray * entry = ...;
// Figure out the dictionary key for the batch.
// (it doesn't have to be a string, this is just for example)
NSString * key = ...;
// Try to retrieve the object for that key
NSMutableArray * entries = _dataDictionary[key];
// If the result is `nil`, the key is not in the dictionary yet.
if( !entries ){
// Create a new mutable array
entries = [NSMutableArray array];
// Add that to the dictionary as the value for the given key
_dataDictionary[key] = entries;
}
// Now `entries` is a valid `NSMutableArray`, whether it already
// existed or was just created. Add this batch.
[entries addObject:entry];
// Move on to the next batch.
}

objc How do I get the string object from this Json array?

This is part of an incoming array:
variantArray: (
(
{
CardinalDirection = "North-West";
DirectionVariantId = "DcCi_1445_171_0_0";
Distance = "2.516606318971459";
RouteName = "Woodsy";
Shape = {
Points = (
{
I want to get the value of DirectionVariantId
I would normally loop and use
NSMutableArray *myString = [variantArray[i] valueForKey:#"DirectionVariantId"];
This isn't working and results in an exception when I try to examine the last character in the string:
NSString *lastChar = [myString substringFromIndex:[myString length] - 1];
This is a new data set for me and I'm missing something..
Thanks for any tips.
Json contain two curly bracket means nested array.
Try:
NSString *myString=[[[variantArray objectAtIndex:0] objectAtIndex:0] objectForKey:#"DirectionVariantId"];
I think you're looking for [variantArray[i] objectForKey:#"DirectionVariantId"];
You'd need to convert the object within your incoming array (variantArray[i]) to a NSDictionary but it might already be judging by your original output.

Parsing Json Output correctly

I am trying to correctly target the elements within the Json Output and I am getting closer but I presume there is a easy and obvious way I am missing.
My Json looks like this with a upper level event.
JSON SNIPPET UPDATED
chat = (
(
{
Key = senderId;
Value = {
Type = 0;
Value = "eu-west-1:91afbc3f-890a-4160-8903-688bf0e9efe8";
};
},
{
Key = chatId;
Value = {
Type = 0;
Value = "eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff";
};
},
{
Key = timestamp;
Value = {
Type = 1;
Value = 1430431197;
};
},
//Continued
I am targeting this level using
NSArray *chat = array[#"chat"];
for ( NSDictionary *theCourse in chat )
{
NSLog(#"---- %#", theCourse);
// I tried the following to target the values
//NSLog(#"chatId: %#", [theCourse valueForKey:#"Key"]);
//NSLog(#"timestamp: %#", theCourse[#"senderId"] );
}
}
I need to parse the value data for each key which if I was using an array would do like [theCourse valueForKey:#"Key"] but I think I may not be going deep enough?
As you would expect, [theCourse valueForKey:#"Key"] gives me the Key values but I need the associate values of those keys.
You can create an easier dictionary:
NSArray *chat = array[#"chat"][0];
NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSDictionary* d in chat)
[newDict setValue:d[#"Value"][#"Value"] forKey:d[#"Key"]];
Now you can use the newDict.
NSLog(#"chatId: %#", [newDict valueForKey:#"chatId"]);

How to get values from nested array/ dictionary iOS 7

I fetch values from dictionary and need to display in UITableView, but everything works fine.
On some spot it stops running and shows thread
-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xbfa7670
The code below, which I used to fetch value..
[NSString stringWithFormat:#"%#",[[pageCat1 valueForKeyPath:#"img3"] objectAtIndex:indexPath.row]]
My values are fetched properly in dictionary but lags to display it?
pageCat (
{
img3 = "http://xxx.in/images/page_cat_img/75x75/4.jpg";
name = "PVC Flexible Wires";
page = (
{
id = {
text = 1;
};
img4 = "http://xxxx.in/images/page_img/75x75/1.jpg";
name = "SINGLE CORE FLEXIBLE WIRES ABOVE 6 SQMM";
},
{
id = {
text = 72;
};
img4 = "http://xxx.in/images/page_img/75x75/72.jpg";
name = "SINGLE CORE FLEXIBLE WIRES BELOW 6 SQMM";
}
);
},
{
img3 = "http://xxx.in/images/page_cat_img/75x75/3.jpg";
name = "Bare Copper Wires";
page = {
id = {
text = 29;
};
img4 = "http://xxx.in/images/page_img/75x75/29.jpg";
name = "Tinned Copper Fuse Wires";
};
},
{
img3 = "http://xxx.in/images/page_cat_img/75x75/48.jpg";
name = "Properties of Wire";
page = {
id = {
text = 85;
};
img4 = "http://xxx.in/images/page_img/75x75/85.jpg";
name = "Wires - Normal, HR - PVC, FR, FRLS & Zero Halogen";
};
}
)
Actually look at the log value, it has array and set of values.. i can't find whether it is in what form..
Can anyone help me to find the solution??
Thanks,
Yazh
it looks like [pageCat1 valueForKeyPath:#"img3"] returns a NSString and not a NSArray like you expect
make sure that it returns a NSArray before applying objectAtIndex:
it seems that pageCat1 is a NSArray so you need to write something like:
NSString *path = pageCat1[0][#"img3"];
...
As the error already tells, [pageCat1 valueForKeyPath:#"img3"] returns a NSString and you are calling objectAtIndex: on it which is not recognized for this class. Obviously, pageCat1 differs from what you expected.
Try NSLog(#"%#", pageCat1); to see what it really looks like.
// Edit
pageCat1 (as seen in your update) is an NSArray that contains items of type NSDictionary. What you really want to do is NSString *imgURL = [[pageCat1 objectAtIndex:indexPath.row] objectForKey:#"img3"];
Explanation:
1. [pageCat1 objectAtIndex:indexPath.row] returns a NSDictionary
2. [__dictionary__ objectForKey:#"img3"] returns the NSString containing your image URL
Actually I used XML data, for that i used third party to parse data. Its all of third party which parsed alternate data as array and other as non-array. Finally I check the array with
isKindOfClass
and convert it into array. Therefore my problem in app solved. :-)
Thanks to all who help me..
Please try this one:
//Assuming json is your main dictionary
NSDictionary *pageCat = [json objectForKey:#"pageCat"];
NSMutableArray *array = [[pageCat valueForKey:#"img3"]mutableCopy];
NSLog(#"Value=%#", [array objectAtIndex:indexPath.row]);

Save Special Character/Swedish/German Characters in NSDictionary

I want to save special characters/german/swedish character in NSDictionary and have to post this data to server, but the data saved in the dictionary is converted to some other format as in console output. I am trying to save this string as different typecasts but not getting.
As NSDictionary's data type is generic, and while sending to POST its sent as in the modified format, I want to save this data in NSDictionary as it is, so that it can be sent in proper format to server and readable at server-end
My code is
NSString *playerName = #"Lëÿlã Råd Sölvê"; // dummy player name
NSLog(#"playerName: %#",playerName);
NSDictionary *postParameters = #{#"playerName1": playerName,
#"playerName2": [NSString stringWithString:playerName],
#"playerName3": [NSString stringWithUTF8String:[playerName UTF8String]],
#"playerName4": [NSString stringWithCString:[playerName UTF8String] encoding:NSASCIIStringEncoding],
#"playerName5": [[NSString alloc] initWithString:playerName],
#"playerName6": [NSString stringWithFormat:#"%#",playerName]};
NSLog(#"postParameters: %#",postParameters);
and output is
playerName: Lëÿlã Råd Sölvê
postParameters: {
playerName1 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName2 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName3 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName4 = "L\U00c3\U00ab\U00c3\U00bfl\U00c3\U00a3 R\U00c3\U00a5d S\U00c3\U00b6lv\U00c3\U00aa";
playerName5 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
playerName6 = "L\U00eb\U00ffl\U00e3 R\U00e5d S\U00f6lv\U00ea";
}
How can I achieve this...
There is nothing wrong with your code.
What you are seeing is an artefact of NSLog and the description method - the former invokes the latter to obtain the textual representation of an object for output. For NSString the string is displayed using Unicode. However for NSDictionary contained strings are displayed using Objective-C Unicode character escape sequences, which have the form '\Uxxxx'.
To assure yourself all is OK you can use:
for (NSString *key in postParameters)
NSLog(#"%# -> %#", key, postParameters[key]);
and everything should display fine (except playerName4 where you mess the string up yourself).

Resources