How to extract NSString from NSDictionary that contains nested JSON - objective-c

I'm trying to get NSStrings from an dictionary but the extracted strings contain the following
<__NSSingleObjectArrayI 0x28205fc00>(
Aaron
)
I expected s plain NSString.
Code:
NSString *name = [jsonDict valueForKeyPath:#"users.fName"];
NSLog(#"name, %#", name);
I though that using 'objectAtIndex' might work but it didn't.
Structure of NSDictionary (JSON) that I'm working with.
{
TYPE = UPDATE;
keys = (
{
calendarId = b306db7e1f924fdebade3813dd596f5d;
delFlag = 0;
editTime = "2019-10-23T12:02:32.0857328";
endTime = "2019-10-24T10:35:00";
insId = 20;
itemKeyId = 15;
keyId = ce71bc7ae3c145adad18a72e56cf0fab;
projectId = 950710ab2b96413cbfd186141e147b3e;
startTime = "2016-05-23T21:10:00";
updateStatus = 1;
validateCount = 6;
}
);
users = (
{
calendarId = 0;
delFlag = 0;
editTime = "2019-10-23T12:02:32.0794244";
fName = Aaron;
insId = 20;
itemUserId = 15;
keyId = ce71bc7ae3c145adad18a72e56cf0fab;
keyMac = 50338B11E912;
lName = Joines;
operId = 30222e0b4b0e4df6b669c3cf69245422;
projectId = 950710ab2b96413cbfd186141e147b3e;
userId = 8be21d1690bb46979a04b4e45a1ba625;
userPin = 123456;
}
);
}

Related

Get the Array list based on the duplicate values in that Array Objective C

i have stuck up with below problem. I have got the below response.
(
{
CarrId = 102;
CarrName = "Fast and Furious";
CarrOptions = (
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
},
{
Id = 10;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "S-30";
},
{
Id = 11;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "M-30";
},
{
Id = 12;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "L-30";
},
{
Id = 13;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "S-30";
},
{
Id = 14;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "M-30";
}
);
Status = 1;
}
)
Get the Array list based on the duplicate values in that Array Objective C. I need to get the array based on the PImg. For example if PImg = 412.jpg. i need the array something lik below
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
}
I have only done like getting the duplicates from the response
NSMutableDictionary * thisRow = [resultArray objectAtIndex:0];
NSMutableArray *pdctDtls = [thisRow objectForKey:#"CarrOptions"];
NSOrderedSet *orderedSet = [[NSOrderedSet orderedSetWithArray:pdctDtls] valueForKey:#"PImg"];
NSMutableArray *newarray = [orderedSet mutableCopy];
In newarray am getting the duplicate values. but if in the newarray if 412.jpg get the array related to 412.jpg. TIA
Try this. If you pass 611.jpg, you get 3 objects.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF CONTAINS[c] %#", #"611.jpg"]; // AS PER YOUR REQUIREMENT
NSArray *array =[pdctDtls filteredArrayUsingPredicate:predicate];
You could use an NSPredicate like so:
NSArray *pdctDtls = thisRow[#"CarrOptions"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"PImg = %#", #"412.jpg"];
NSArray *resultsWithSamePimg = [pdctDtls filteredArrayUsingPredicate:predicate];
if (resultsWithSamePimg.count > 1) {
// we have some duplicates
// ...
}
But I would rather use a plain old for loop, which might be easier to debug and understand.

Get 1 Segment of a Array

I want to get from my Array only on section.
This is my result...
(
{
foreName = FORENAME1;
gender = female;
id = 15;
key = 12345678901;
longName = LASTNAME1;
name = FORELAST1;
},
{
foreName = FORENAME2;
gender = male;
id = 52;
key = 12345678902;
longName = LASTNAME2;
name = FORELAST2;
},
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
)
How can I search for id 77 and get the whole entry array for id 77 like the following example?
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
That result is an array, and you want the element who's id==77...
- (NSDictionary *)elementInArray:(NSArray *)array withId:(NSNumber *)anId {
// there are a lot prettier ways, but this will be the clearest
for (NSDictionary *d in array) {
if ([d[#"id"] isEqual:anId]) return d;
}
return nil;
}
// call it like this
NSArray *array = // the array logged in the OP
NSDictionary result = [self elementInArray:array withId:#77];
NSLog(#"%#", result);

AFNetworking post request fails due to nsdictionary value with double quotes

When I set nsdictionary as request parameters, dictionary values with special character are added with double quotes and that cause the post request to failed but when I use same dictionary values using "Rest client (Chrome Add-ons)" request post successfully. How to fix the issue?
for example lat and long should be float value because of special character double quotes are added automatically. Here is my NSDictionary log->
**
AptSuiteNo = 20;
BuildingName = "the ";
BuiltInYear = 2014;
BuiltUpAreaSize = 200;
CityName = Dhaka;
CountryName = Bangladesh;
CreateDate = "19-04-2016";
CreatedByCompanyID = 7746;
CreatedByUserID = 7915;
CurrencyID = 1;
Description = "The ";
LastUpdateDate = "19-04-2016";
LastUpdatedByUserID = 7915;
Latitude = "23.832637";
LocalityName = Cantonment;
Longitude = "90.416590";
NeighborhoodName = "Nikunja 2";
PlaceID = ChIJieh0al7GVTcRvMjqALwGYVc;
PostedBy = owner;
Price = 200;
PropertyBathroomNumber = 1;
PropertyBedroomNumber = 1;
PropertyBuildUpAreaSizeTypeID = 1;
PropertyID = 0;
PropertyLotSize = 250;
PropertyLotSizeTypeID = 1;
PropertyName = "the ";
PropertyTransactionTypeCategoryID = 1;
PropertyTransactionTypeID = 1;
PropertyTypeID = 10;
StateName = "Dhaka Division";
StateTicker = "Dhaka Division";
ZipCode = 1229;
**
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:#"POST" URLString:RequestURL parameters:[self requestDataString] error:&error];

How to extract data values and added to tableview

How to extract the data from the below data structure, and display on tableview
id = extractData
[extractData objectForKey:#"data"] //BELOW IS OUTPUT:
( {
Name = {
id = 2;
name = rat;
};
Place = {
id = 7;
name = New York;
};
Item = {
Times = "100";
“expire” = 10-19-2015;
“value” = 300;
id = 1;
name = newname;
“checked” = 0;
};
Type = {
id = 1;
name = round;
};
},
{
Name = {
id = 2;
name = norway;
};
Place = {
id = 3;
name = ops;
};
Item = {
Times = "100";
“expire” = 10-18-2015;
“value” = 300;
id = 1;
name = new;
“checked” = 1;
};
Type = {
id = 2;
name = square;
};
}
)
Want to display Name,Place,Item,Type values in tableview cell with each iterations.
Can any one advice, how can extract value from above data.
Under your cellForRowAtIndexPath after initializing the custom cell add this code :
NSDictionary *dicVal = [yourDataArray objectAtIndex:indexPath.row];
cell.labelName.text = [[dicVal objectForKey:#"Name"] objectForKey:#"name"];
cell.labelPlace.text = [[dicVal objectForKey:#"Place"] objectForKey:#"name"];
cell.labelItem.text = [[dicVal objectForKey:#"Item"] objectForKey:#"name"];
cell.labelType.text = [[dicVal objectForKey:#"Type"] objectForKey:#"name"];
return yourCustomCell;
This should do the work. Thanks.

How to get exif information from ALAsset?

I need to get exif information from selected asset and to display part of it on the screen.
[[asset defaultRepresentation] metadata];
returns a dictionary of dictionaries of metadata for the given asset representation. Check out this dictionary for the data you're interested in. I am not sure but I think the keys in that dictionary (and subdictionaries) are the same as the ones listed under CGImageProperties Reference.
This is a complete answer ... all in one spot
NSDictionary *nsd = [[asset3 defaultRepresentation] metadata];
NSString *widthStr = [nsd objectForKey: #"PixelWidth"];
NSString *heightStr = [nsd objectForKey: #"PixelHeight"];
NSLog(#"nsd: %# ... widthStr: %# ... heightStr: %# ...", nsd, widthStr, heightStr);
output:
nsd: {
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 1;
PixelHeight = 1136;
PixelWidth = 642;
"{Exif}" = {
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
ExifVersion = (
2,
2,
1
);
FlashPixVersion = (
1,
0
);
PixelXDimension = 642;
PixelYDimension = 1136;
SceneCaptureType = 0;
};
"{TIFF}" = {
Orientation = 1;
ResolutionUnit = 2;
XResolution = 72;
YResolution = 72;
};
} ... widthStr: 642 ... heightStr: 1136 ...