I will get the JSON Data like this. But I want to check whether key has an Array data or not. If It has 0 then skip. How can I check?
code = 200;
data = {
UserstoreReviewDetails = 0;
storeReviewDetails = 0;
}
or
code = 200;
data = {
UserstoreReviewDetails = (
{// array data}
);
storeReviewDetails = (
{// array data}
);
}
I have tried like this. But getting an exception if data has an Array value.
[[self.jsonValue[#"data"] objectForKey:#"storeReviewDetails"]intValue] != 0
[[self.jsonValue[#"data"] objectForKey:#"storeReviewDetails"] isEqualToString:#"0"]
// try like this
if([[[self.jsonValue[#"data"] objectForKey:#"storeReviewDetails"] isKindOfClass:[NSArray class]]) {
// data is array type
} else {
//data is not an array type
}
Try like this.
NSArray *array = [[self.jsonValue[#"data"] objectForKey:#"storeReviewDetails"];
if (array) {
NSLog("StoreReviewDetails is array");
}
else {
int count = [[self.jsonValue[#"data"] objectForKey:#"storeReviewDetails"]intValue];
}
Related
I have JSON data set returned that I am parsing.
If the data contains a record it looks like this:
dictData: {
Recordset = (
{
Record = (
{
MyDate = "2016-06-15 11:04:43";
MyID = 53070;
SomeDescription = "";
}
);
}
);
}
If the data does NOT contains a record it looks like this:
Notice the "" in the return set which is causing me issues.
dictData: {
Recordset = (
""
);
}
I am having trouble accounting for the zero length array in the Recordset.
Here is my base code for parsing the data.
NSDictionary * dictData = [NSDictionary dictionaryWithDictionary:[someOtherDictionary objectForKey:#"Data"]];
NSArray * arrRecordSet = [dictData objectForKey:#"Recordset"];
if([arrRecordSet objectAtIndex:0] != nil) {
NSLog(#"CONTAINS RECORDS");
//Do something with the records
}
else {
NSLog(#"DOES NOT CONTAIN RECORDS");
}
I have tried various iterations of things like checking if [arrRecordSet objectAtIndex:0] length is greater than zero or checking the count size.
I think the double quotes are throwing me off.
Any help?
UPDATE
RAW JSON as requested
ONE RECORD
{
APIResponse = {
Data = {
Recordset = (
{
Record = (
{
MyDate = "2016-06-15 11:04:43";
MyID = 53070;
SomeDescription = "";
}
);
}
);
};
};
}
NO RECORDS
{
APIResponse = {
Data = {
Recordset = (
""
);
};
};
}
So in the case of success the Recordset value is an array with a dictionary and in the case of failure the Recordset value is an array with a string.
So:
NSArray *recordSet = response[#"APIResponse"][#"Data"][#"Recordset"];
if ([recordSet count] > 0 &&
[recordSet[0] isKindOfClass:[NSDictionary class]]) {
// Success
NSDictionary *record = recordSet[0];
...
} else {
// Failure
}
Here you should check first all key for dictionary Recordset if it returns you count>0 then you can say data is available and if not then there is a blank entry.
I receive following NSDictionary from my server. I don't know how to access each row and put them in UITableView. I need a for loop or something to access each of them one by one: I get following when I say:
NSLog(#"%#",dict);
{
chores = (
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 1;
name = TEST;
parent = Blah;
"previous_chores" = (
);
},
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 2;
name = TEST2;
parent = Blah;
"previous_chores" = (
);
},
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 3;
name = TEST3;
parent = Blah;
"previous_chores" = (
);
}
);
}
I guess you can directly use the array without split it like following:
Suppose you have an array with dictionary and inside the dictionary as your data and kiran said:
NSArray *arrChores = [dict valueForKey #"Chores"];
Your array look like 0th index is:
{
"first_due_date" = "2016-03-12";
frequency = weekly;
id = 1;
name = TEST;
parent = Blah;
"previous_chores" = (
);
}
so you can print the name like following in cellForrowIndex:
NSLog(#"=== %#",[[arrChores objectAtIndex:indexPath.row]valueForKey:#"name"])
More easy and less maintain :)
Create a model for Chores.
Looking at the dictionary.
Chores is an array consisting of dictionaries in it.
So you can have array like this
arrChoresGlobal = [NSMutableArray new];
NSArray *arrChores = [dict valueForKey #"Chores"];
Then iterate this array and have a model.
like below
for(NSDictionary *dctChore in arrChores)
{
SomeModel *obj = [SomeModel new];
obj.first_due_date = [dctChore valueForKey:#"first_due_date"];
[arrChoresGlobal addObject:obj];
}
Then use this array count in numberOfRows in tableview
return [arrChoresGlobal count];
and in cellForRowAtIndexPath
SomeModel *obj = [arrChoresGlobal objectAtIndex:indexPath.row];
Edit :- Longer but systematic way :)
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"]);
I got an array:
NSArray *itemsArray = [self.tournamentDetails.groups valueForKey:#"Items"];
Where self.tournamentDetails.groups is an NSArray, build with a JSON string from a webRequest.
Items is sometimes empty and sometimes it contains objects. So i need an if statement to check if its empty or not. I've tried some different things like:
if ([itemsArray count]!=0)
if (!itemsArray || !itemsArray.count)
Problem is that if my Items object from valueForKey is empty then the itemsArray still contains an object looking like this
<__NSArrayI 0x178abef0>(
<__NSArrayI 0x16618c30>(
)
)
When theres items inside my Items object it looks like this:
<__NSArrayI 0x18262b70>(
<__NSCFArray 0x181e3a40>(
{
BirthDate = 19601006T000000;
ClubName = "Silkeborg Ry Golfklub";
ClubShortName = "";
CompletedResultSum = {
Actual = {
Text = 36;
Value = 36;
};
ToPar = {
Text = "";
Value = 0;
};
};
}
)
)
Meaning that [itemsArray count] is always equal to 1 or more, and then it jumps into the if statement when it should not.
Anyone know how i can create an if statement where if itemsArray contains "Items:[]" it will be skipped and if itemsArray contains "Items:[lots of objects]" it will run?
EDIT: Solution is to check up on the first index like this if([[itemsArray objectAtIndex:0] count] != 0) then run code.
Try this:
if(itemsArray && itemsArray.count>0) //be sure that it has value){
for(NSArray *item in itemsArray){
if(item.count > 0){
// you have an NSDictionary. Will process it
}else{
//item.count == 0 : this is an empty NSArray.
}
}
}
more simpler
for (id obj in itemsArray)
{
if([obj isKindOfClass:[NSArray class]])
{
if(obj.count > 0)
{
//your if condition here
}
}
}
I am using this code: https://github.com/valentinlietz/MySQL-Connect to use MySQL queries in objective c. I use Count(*) to check if this row already exists.
I used this code to NSLog(); this dictionary. Code:
for(NSArray *row in response.responseDictionary){
NSLog(#"%#", row);
}
Result:
{
"" = "<null>";
"COUNT(*)" = 0;
}
But when I use [response.responseDictionary objectForKey:#"COUNT(*)"]; It's returning Unrecognized selector sent to instance.
Update:
Row:
Row: ( { "" = "<null>"; "COUNT()" = 0; } )
Dictionary:
Row: { "" = "<null>"; "COUNT()" = 0; }
I haven't worked with that project, but from https://github.com/valentinlietz/MySQL-Connect/blob/master/MySQL.m it looks to me that response.responseDictionary is the result
of converting the JSON response of the script https://github.com/valentinlietz/MySQL-Connect/blob/master/mysql.php to a Foundation object.
And the code
while($row = mysql_fetch_array($query))
{
for($i = 0; $i < 50; $i++) {
$arr2[$fields[$i]] = $row[$fields[$i]];
}
array_push($return_arr, $arr2);
}
echo json_encode($return_arr);
in that PHP script shows that the response is an array of dictionaries.
So if I am correct, the name and type of the responseDictionary property is badly chosen, because it is not an NSDictionary, but an NSArray where each object is an NSDictionary,
and the following should work:
NSArray *responseArray = (NSArray *)response.responseDictionary;
NSLog(#"count = %#", [[responseArray objectAtIndex:0] objectForKey:#"COUNT(*)"]);