Convert NSArray to NSDictoonary in a specific format - ios

I have a NSArray having below data and want to convert it to NSDictionary in the format that follows. The array has only [0] and the data belongs to that index only. The reason why the dictionary should have this format is a web service that accepts only this format.
The array fetches data from Core DB and then i have to send it to the web service.
<MatchDayChecklist: 0xa6a04c0> (entity: MatchDayChecklist; id: 0xa69eee0 <x-coredata://2A66067E-8B0D-4BAC-857D-E6C71906270C/MatchDayChecklist/p2> ; data: {
acceptDeclaration = 1;
actions = Df;
authorisedRep = 1;
awayAssociationId = 18;
awayClubId = 7;
awayTeamName = Sdf;
awayTeamRepName = Df;
awayTeamRepPosition = df;
createDate = "2014-03-19 09:09:13 +0000";
firstAidFacilityAccessible = 1;
groundMarkingsAcceptable = 0;
homeAssociationId = 18;
homeClubId = 7;
homeTeamName = E;
homeTeamRepName = Df;
homeTeamRepPosition = Df;
inspectionDate = "2014-03-19 09:08:00 +0000";
matchDayChecklistid = 1;
matchVenue = Sdf;
perimeterFencesFreeOfHazard = 1;
photo = "<relationship fault: 0xa7ae630 'photo'>";
photoThumbs = "<relationship fault: 0xa7ae880 'photoThumbs'>";
playSurfaceAcceptable = 0;
playerAreaFreeOfHazard = 0;
publicAreaFreeOfHazard = 0;
sprinkleCoverLevelled = 0;
stateAwayTeam = NSW;
stateHomeTeam = NSW;
submited = 2;
updateDate = "2014-03-19 09:09:13 +0000";
visibleDebrisRemoved = 0;
weatherSafeToPlay = 1;
})
Format of Dictionary:
#"homeAssociationID": 22,
#"homeClubID": 7,
#"homeTeamName": df,
#"awayAssociationID": 4,
#"awayClubID": 4,
#"awayTeamName": sfd,
#"matchVenue": sdf,
#"inspectionDateTime": 2014-03-19 09:08:00 +0000,
#"weatherSafeToPlay": 0,
#"playSurfaceAcceptable": 0,
#"visibleDebrisRemoved": 0,
#"groundMarkingsAcceptable": 0,
#"sprinklerCoverLevelled": 0,
#"perimeterFencesFreeOfHazard": 1,
#"publicAreaFreeOfHazard": 1,
#"playerAreaFreeOfHazard": 1,
#"firstAidFacilityAccessible": 0,
#"actions": (includeActionsAndPhotos ? actions : #""),
#"authorisedRep": 0,
#"acceptDeclaration": 1,
#"homeTeamRepName": sdf,
#"homeTeamRepPosition": sdf,
#"awayTeamRepName": df,
#"awayTeamRepPosition": sdf,
#"photos":
encodedImageArray,
please help.

Related

Split into string array only the key by comma and not values c#

This is my String and i have problems when splitting into string array with comma seperated values for keys
{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }
when i try to use string.Split(',') i get "Ucomment = tet","tet1" as seperate array.
But i need to have split string[] when seperated by comma
UComment = tet,tet1
OComment = test,test1
I have tried using the regex ,(?=([^\"]\"[^\"]\")[^\"]$)" but it didnot work.
You may try matching on the regex pattern \S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$):
string input = "{ Yr = 2019, Mth = DECEMBER , SeqN = 0, UComment = tet,tet1, OComment = test,test1, FWkMth = WK, FSafety = Y, FCustConsign = Y, FNCNRPull = 0, FNCNRPush = 0, CreatedTime = 2020-01-03 06:16:53 }";
Regex regex = new Regex(#"\S+\s*=\s*.*?(?=\s*,\s*\S+\s*=|\s*\}$)");
var results = regex.Matches(input);
foreach (Match match in results)
{
Console.WriteLine(match.Groups[0].Value);
}
This prints:
Yr = 2019
Mth = DECEMBER
SeqN = 0
UComment = tet,tet1
OComment = test,test1
FWkMth = WK
FSafety = Y
FCustConsign = Y
FNCNRPull = 0
FNCNRPush = 0
CreatedTime = 2020-01-03 06:16:53
Here is an explanation of the regex pattern used:
\S+ match a key
\s* followed by optional whitespace and
= literal '='
\s* more optional whitespace
.*? match anything until seeing
(?=\s*,\s*\S+\s*=|\s*\}$) that what follows is either the start of the next key/value OR
is the end of the input

Most optimized way to get value from table

I need to check if first value is >= 'from' and second value is <= 'to', if true then my function retun number. It's working but I don't know if this is the best and most optimized way to get value(number from table).
local table = {
{from = -1, to = 12483, number = 0},
{from = 12484, to = 31211, number = 1},
{from = 31212, to = 53057, number = 2},
{from = 53058, to = 90200, number = 3},
{from = 90201, to = 153341, number = 4},
{from = 153342, to = 443162, number = 5},
{from = 443163, to = 753380, number = 6},
{from = 753381, to = 1280747, number = 7},
{from = 1280748, to = 2689570, number = 8},
{from = 2689571, to = 6723927, number = 9},
{from = 6723928, to = 6723928, number = 10}
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local number = 0
for k, v in pairs(table) do
if (v.from and exampleFromValue >= v.from) and (v.to and exampleToValue <= v.to) then
number = v.number
break
end
end
return number
end
print(getNumber())
With this small amount of data, such function doesn't seem like a performace issue. However, you can compress the data a bit:
local t = {
12484, 31212, 53058, 90201, 153342, 443163, 753381, 1280748, 2689571, 6723928
}
local exampleFromValue = 31244
local exampleToValue = 42057
local function getNumber()
local last = -1
for i, v in ipairs(t) do
if exampleFromValue >= last and exampleToValue < v then
return i - 1
end
last = v
end
return 0
end

Mean/median/mode from complex table in lua

Thank you for all the support with lua, I'm very new and my application is working nicely so far.
I've made an app that will take in a few thousand numbers for different items. I'm trying to find the Mean/Media/Mode for each item, I've been able to do this for a single item, but not for all items.
Here is my table structure:
for i = 0, 1500 do
local e = {}
e.seller,
e.buyer,
e.itemName,
e.soldAmount = GetSoldAmount(FromMember(i))
table.insert(allSalesTempTable, e)
end
Table output format
[1] =
{
["itemName"] = [[Salad]]
["buyer"] = [[#Mike]],
["eventType"] = 15,
["soldAmount"] = 150,
["seller"] = [[#Sarah]],
},
[2] =
{
["itemName"] = [Pizza]
["buyer"] = [[#James]],
["eventType"] = 15,
["soldAmount"] = 150,
["seller"] = [[#Sarah]],
},
[3] =
{
["itemName"] = [Salad]
["buyer"] = [[#Frank]],
["eventType"] = 15,
["soldAmount"] = 75,
["seller"] = [[#Sarah]],
},
[4] ...
},
Then I'm trying to send the table/array to this mean function
stats={}
-- Get the mean value of a table
function stats.mean( t )
local sum = 0
local count= 0
local tempTbl = {}
(This is completely not going to work, but its what I'm trying so far)
for k,v in pairs(t) do tempTbl[k] = v
if v.itemName == tempTbl.itemName then
sum = sum + v.soldAmount
count = count + 1
end
end
return (sum / count)
end
--- To get the function started
stats.mean(e)
Here is where I'm getting fuzzy, Not sure if I can add the MEAN while collecting the data into the first temp table, or if it needs to be re-calculated after I have all the data?
If it needs to be done after, then my stats.mean(e) needs a way to insert it?
I'm trying to get this output:
[1] =
{
["itemName"] = [[Salad]]
["buyer"] = [[#Mike]],
["eventType"] = 15,
["soldAmount"] = 150,
["seller"] = [[#Sarah]],
["mean"] = 112.5 - New Insert somehow
},
[2] =
{
["itemName"] = [Pizza]
["buyer"] = [[#James]],
["eventType"] = 15,
["soldAmount"] = 150,
["seller"] = [[#Sarah]],
["mean"] = 150 - New Insert somehow
},
[3] =
{
["itemName"] = [Salad]
["buyer"] = [[#Frank]],
["eventType"] = 15,
["soldAmount"] = 75,
["seller"] = [[#Sarah]],
["mean"] = 112.5 - New Insert somehow
},
[4] ...
},
I've been working on this problem for a few days, After I see how to adjust my format for the mean and insert into the existing table I'll be able to figure out mean/min/max/median/mode easy enough.

How to parse this string received from TMDB in iOS

I am getting the popular movie data from TMDB in my iOS App. however i am having great trouble in parsing and getting the meaningful data from it. I am totally new to iOS. i have done similar thing in Windows Phone, where i created Poco's and used DataContractJsonSerializer to parse the data.
but i am not getting any idea on how to do this in iOS.
I want to retrieve id, original_title and poster path from this string.
this is the data i am getting
{
buffer = {
0 = 123;
1 = 34;
10 = 34;
100 = 114;
3451 = 34;
3452 = 58;
3453 = 52;
3454 = 54;
3455 = 46;
3456 = 55;
3457 = 50;
3458 = 49;
3459 = 54;
346 = 110;
3460 = 49;
3461 = 48;
// lot of number in between
977 = 87;
978 = 75;
979 = 68;
98 = 34;
980 = 108;
981 = 51;
982 = 88;
983 = 97;
984 = 98;
985 = 99;
986 = 112;
987 = 72;
988 = 82;
989 = 110;
99 = 111;
990 = 110;
991 = 76;
992 = 77;
993 = 75;
994 = 102;
995 = 85;
996 = 46;
997 = 106;
998 = 112;
999 = 103;
length = 5810;
};
};
cookies = {
};
data = {
page = 1;
results = (
{
adult = 0;
"backdrop_path" = "/AdRL6c4BoMJgk7ZFUB2oUVzav2p.jpg";
id = 41602;
"original_title" = "The Necessary Death of Charlie Countryman";
popularity = "110.726084305053";
"poster_path" = "/fSwdCCGmO50IMaH4XMAixjclLDF.jpg";
"release_date" = "2013-11-15";
title = "The Necessary Death of Charlie Countryman";
"vote_average" = "7.5";
"vote_count" = 37;
},
{
adult = 0;
"backdrop_path" = "/mrvlpJFAzKwZZkLm9VD7Rh2VECi.jpg";
id = 116745;
"original_title" = "The Secret Life of Walter Mitty";
popularity = "64.2212199623452";
"poster_path" = "/v3e1LdwTXupH9L78eIWCKBjclhJ.jpg";
"release_date" = "2013-12-25";
title = "The Secret Life of Walter Mitty";
"vote_average" = "7.2";
"vote_count" = 124;
},
{
adult = 0;
"backdrop_path" = "/8aZHR0wXacn5DVYK3cS2ozWYPCN.jpg";
id = 64686;
"original_title" = "47 Ronin";
popularity = "95.9101659814315";
"poster_path" = "/v9JCVROrdlHZCWP3D6pnV8Xc29w.jpg";
"release_date" = "2013-12-25";
title = "47 Ronin";
"vote_average" = "6.5";
"vote_count" = 81;
},
{
adult = 0;
"backdrop_path" = "/zZTyJ6fbWKDl3XabcpHRnnLMKfU.jpg";
id = 249397;
"original_title" = "Nymphomaniac: Vol. II";
popularity = "94.03616025076791";
"poster_path" = "/pCW6krILJ2L0rXDXH0715teKTtm.jpg";
"release_date" = "2014-03-20";
title = "Nymphomaniac: Vol. II";
"vote_average" = "6.8";
"vote_count" = 11;
},
{
adult = 0;
"backdrop_path" = "/hyR7Fs6Tepgu3yCQGtgO4Ilz9tY.jpg";
id = 57158;
"original_title" = "The Hobbit: The Desolation of Smaug";
popularity = "88.3637510164635";
"poster_path" = "/gQCiuxGsfiXH1su6lp9n0nd0UeH.jpg";
"release_date" = "2013-12-13";
title = "The Hobbit: The Desolation of Smaug";
"vote_average" = "7.6";
"vote_count" = 434;
},
{
adult = 0;
"backdrop_path" = "/cAhCDpAq80QCeQvHytY9JkBalpH.jpg";
id = 109445;
"original_title" = Frozen;
popularity = "69.4628746979819";
"poster_path" = "/jIjdFXKUNtdf1bwqMrhearpyjMj.jpg";
"release_date" = "2013-11-19";
title = Frozen;
"vote_average" = "7.7";
"vote_count" = 348;
},
{
adult = 0;
"backdrop_path" = "/rP36Rx5RQh0rmH2ynEIaG8DxbV2.jpg";
id = 106646;
"original_title" = "The Wolf of Wall Street";
popularity = "64.6183486216064";
"poster_path" = "/wAgdJRx4uZ0u4uzu34NOMvtjLAR.jpg";
"release_date" = "2013-12-25";
title = "The Wolf of Wall Street";
"vote_average" = "7.9";
"vote_count" = 330;
},
{
adult = 0;
"backdrop_path" = "/r7Lmi2Jj1CJLdipYtLEU5iA4SB5.jpg";
id = 64807;
"original_title" = "Grudge Match";
popularity = "59.9853953814402";
"poster_path" = "/vzIIna3nvQAVGBBXbZgzvPSxg36.jpg";
"release_date" = "2013-12-25";
title = "Grudge Match";
"vote_average" = "6.6";
"vote_count" = 16;
},
{
adult = 0;
"backdrop_path" = "/mnxWdWTP3jbfxC4oaPrwevwvOZ2.jpg";
id = 53182;
"original_title" = "300: Rise of an Empire";
popularity = "49.8372271195572";
"poster_path" = "/d4kPMHsoTEH3FIkBDJM0uVOlas6.jpg";
"release_date" = "2014-03-07";
title = "300: Rise of an Empire";
"vote_average" = "6.6";
"vote_count" = 102;
},
{
adult = 0;
"backdrop_path" = "/rO75nODBBmJx4u5ZRy2BsGFgbO7.jpg";
id = 177494;
"original_title" = "Veronica Mars";
popularity = "48.7870515185193";
"poster_path" = "/nS3L07mQfcNJcisLEKgi8fWoBS1.jpg";
"release_date" = "2014-03-14";
title = "Veronica Mars";
"vote_average" = "7.2";
"vote_count" = 19;
},
{
adult = 0;
"backdrop_path" = "/iJtq3PHsLgjcYIrNlT2glzEdBo5.jpg";
id = 110415;
"original_title" = Snowpiercer;
popularity = "47.3945315956173";
"poster_path" = "/3J4QoMpQYE2MehOTQG9X2KUP4aq.jpg";
"release_date" = "2013-08-01";
title = Snowpiercer;
"vote_average" = "7.2";
"vote_count" = 29;
},
{
adult = 0;
"backdrop_path" = "/qMDiCjxfv6Y8JN2DFViTX5D1ORH.jpg";
id = 24253;
"original_title" = "Flickan som lekte med elden";
popularity = "46.7216104479466";
"poster_path" = "/qHRpU2d9NWB0WDulwgFwg6a9JRK.jpg";
"release_date" = "2009-09-18";
title = "The Girl Who Played with Fire";
"vote_average" = "6.8";
"vote_count" = 141;
},
{
adult = 0;
"backdrop_path" = "/1DfcGAQ4EVIZFnveo1IzHFtgFTS.jpg";
id = 175112;
"original_title" = "The Pirate Fairy";
popularity = "46.03463240154";
"poster_path" = "/6VmPnBPDCTbpZ3Jj5lbgHD10IZm.jpg";
"release_date" = "2014-04-01";
title = "The Pirate Fairy";
"vote_average" = "7.6";
"vote_count" = 5;
},
{
adult = 0;
"backdrop_path" = "/1RTiQXeHoEMXkZNWaB8W5uaEZ2.jpg";
id = 205220;
"original_title" = Philomena;
popularity = "45.1210557523094";
"poster_path" = "/6BTXHupSPkrwsoz4Br6qwwSVmhj.jpg";
"release_date" = "2013-11-27";
title = Philomena;
"vote_average" = "7.5";
"vote_count" = 31;
},
{
adult = 0;
"backdrop_path" = "/wRCPG1lsgfTFkWJ7G3eWgxCgv0C.jpg";
id = 101299;
"original_title" = "The Hunger Games: Catching Fire";
popularity = "41.4568100606251";
"poster_path" = "/tAhSyLxpaZJCr1oc2a3flvC2B7x.jpg";
"release_date" = "2013-11-22";
title = "The Hunger Games: Catching Fire";
"vote_average" = "7.7";
"vote_count" = 518;
},
{
adult = 0;
"backdrop_path" = "/hz3JfAikYXtaNWIJhWM4p5sy5OZ.jpg";
id = 49047;
"original_title" = Gravity;
popularity = "40.0573419755177";
"poster_path" = "/2gPjLWIyrWlAn2DgKMOKTBnZYyO.jpg";
"release_date" = "2013-10-04";
title = Gravity;
"vote_average" = "7.9";
"vote_count" = 751;
},
{
adult = 0;
"backdrop_path" = "/3FweBee0xZoY77uO1bhUOlQorNH.jpg";
id = 76338;
"original_title" = "Thor: The Dark World";
popularity = "38.5511611536454";
"poster_path" = "/aROh4ZwLfv9tmtOAsrnkYTbpujA.jpg";
"release_date" = "2013-11-08";
title = "Thor: The Dark World";
"vote_average" = "7.1";
"vote_count" = 503;
},
{
adult = 0;
"backdrop_path" = "/dNPmXYRS3nN4vD7MLtz5lP79DCB.jpg";
id = 11824;
"original_title" = "Teen Wolf";
popularity = "37.546317184227";
"poster_path" = "/3TKJbKNpHvRP8YVnwbgfok41AAC.jpg";
"release_date" = "1985-08-23";
title = "Teen Wolf";
"vote_average" = "7.1";
"vote_count" = 31;
},
{
adult = 0;
"backdrop_path" = "/kJzvjhJP6Xf7QQofVl3y0NvpwmI.jpg";
id = 256731;
"original_title" = "Bad Country";
popularity = "37.123709776744";
"poster_path" = "/6bjfGrtUYmuZzFCia3TcvY0Kz1e.jpg";
"release_date" = "2014-03-10";
title = "Bad Country";
"vote_average" = "5.5";
"vote_count" = 3;
},
{
adult = 0;
"backdrop_path" = "/6Ace8kIosYGnAiJUHgbLO4MNI6k.jpg";
id = 77067;
"original_title" = DeadHeads;
popularity = "36.41";
"poster_path" = "/A7kD47MEXywqKPeKHrxBfkvPTqy.jpg";
"release_date" = "2011-04-29";
title = DeadHeads;
"vote_average" = "5.8";
"vote_count" = 6;
}
);
"total_pages" = 7848;
"total_results" = 156953;
};
headers = {
"Access-Control-Allow-Origin" = "*";
Age = 3350;
"Cache-Control" = "public, max-age=14400";
Connection = "keep-alive";
"Content-Length" = 5810;
"Content-Type" = "application/json;charset=utf-8";
Date = "Sat, 29 Mar 2014 10:14:03 GMT";
ETag = "\"76070c8ebb216a66cbbc36e56c1407fa\"";
Server = nginx;
Status = "200 OK";
Vary = "Accept-Encoding";
Via = "1.0 localhost (squid/3.1.19)";
"X-Apiary-Ratelimit-Limit" = 120;
"X-Apiary-Ratelimit-Remaining" = 119;
"X-Apiary-Transaction-Id" = 53369cebed260702000005cc;
"X-Cache" = "HIT from localhost";
"X-Cache-Lookup" = "HIT from localhost:3128";
"X-Memc" = HIT;
"X-Memc-Age" = 12648;
"X-Memc-Expires" = 1752;
"X-Memc-Key" = eb13032fb1ef09086dcaac2d14c098c0;
};
status = 200;
text = "{\"page\":1,\"results\":[{\"adult\":false,\"backdrop_path\":\"/AdRL6c4BoMJgk7ZFUB2oUVzav2p.jpg\",\"id\":41602,\"original_title\":\"The Necessary Death of Charlie Countryman\",\"release_date\":\"2013-11-15\",\"poster_path\":\"/fSwdCCGmO50IMaH4XMAixjclLDF.jpg\",\"popularity\":110.726084305053,\"title\":\"The Necessary Death of Charlie Countryman\",\"vote_average\":7.5,\"vote_count\":37},{\"adult\":false,\"backdrop_path\":\"/mrvlpJFAzKwZZkLm9VD7Rh2VECi.jpg\",\"id\":116745,\"original_title\":\"The Secret Life of Walter Mitty\",\"release_date\":\"2013-12-25\",\"poster_path\":\"/v3e1LdwTXupH9L78eIWCKBjclhJ.jpg\",\"popularity\":64.2212199623452,\"title\":\"The Secret Life of Walter Mitty\",\"vote_average\":7.2,\"vote_count\":124},{\"adult\":false,\"backdrop_path\":\"/8aZHR0wXacn5DVYK3cS2ozWYPCN.jpg\",\"id\":64686,\"original_title\":\"47 Ronin\",\"release_date\":\"2013-12-25\",\"poster_path\":\"/v9JCVROrdlHZCWP3D6pnV8Xc29w.jpg\",\"popularity\":95.9101659814315,\"title\":\"47 Ronin\",\"vote_average\":6.5,\"vote_count\":81},{\"adult\":false,\"backdrop_path\":\"/zZTyJ6fbWKDl3XabcpHRnnLMKfU.jpg\",\"id\":249397,\"original_title\":\"Nymphomaniac: Vol. II\",\"release_date\":\"2014-03-20\",\"poster_path\":\"/pCW6krILJ2L0rXDXH0715teKTtm.jpg\",\"popularity\":94.0361602507679,\"title\":\"Nymphomaniac: Vol. II\",\"vote_average\":6.8,\"vote_count\":11},{\"adult\":false,\"backdrop_path\":\"/hyR7Fs6Tepgu3yCQGtgO4Ilz9tY.jpg\",\"id\":57158,\"original_title\":\"The Hobbit: The Desolation of Smaug\",\"release_date\":\"2013-12-13\",\"poster_path\":\"/gQCiuxGsfiXH1su6lp9n0nd0UeH.jpg\",\"popularity\":88.3637510164635,\"title\":\"The Hobbit: The Desolation of Smaug\",\"vote_average\":7.6,\"vote_count\":434},{\"adult\":false,\"backdrop_path\":\"/cAhCDpAq80QCeQvHytY9JkBalpH.jpg\",\"id\":109445,\"original_title\":\"Frozen\",\"release_date\":\"2013-11-19\",\"poster_path\":\"/jIjdFXKUNtdf1bwqMrhearpyjMj.jpg\",\"popularity\":69.4628746979819,\"title\":\"Frozen\",\"vote_average\":7.7,\"vote_count\":348},{\"adult\":false,\"backdrop_path\":\"/rP36Rx5RQh0rmH2ynEIaG8DxbV2.jpg\",\"id\":106646,\"original_title\":\"The Wolf of Wall Street\",\"release_date\":\"2013-12-25\",\"poster_path\":\"/wAgdJRx4uZ0u4uzu34NOMvtjLAR.jpg\",\"popularity\":64.6183486216064,\"title\":\"The Wolf of Wall Street\",\"vote_average\":7.9,\"vote_count\":330},{\"adult\":false,\"backdrop_path\":\"/r7Lmi2Jj1CJLdipYtLEU5iA4SB5.jpg\",\"id\":64807,\"original_title\":\"Grudge Match\",\"release_date\":\"2013-12-25\",\"poster_path\":\"/vzIIna3nvQAVGBBXbZgzvPSxg36.jpg\",\"popularity\":59.9853953814402,\"title\":\"Grudge Match\",\"vote_average\":6.6,\"vote_count\":16},{\"adult\":false,\"backdrop_path\":\"/mnxWdWTP3jbfxC4oaPrwevwvOZ2.jpg\",\"id\":53182,\"original_title\":\"300: Rise of an Empire\",\"release_date\":\"2014-03-07\",\"poster_path\":\"/d4kPMHsoTEH3FIkBDJM0uVOlas6.jpg\",\"popularity\":49.8372271195572,\"title\":\"300: Rise of an Empire\",\"vote_average\":6.6,\"vote_count\":102},{\"adult\":false,\"backdrop_path\":\"/rO75nODBBmJx4u5ZRy2BsGFgbO7.jpg\",\"id\":177494,\"original_title\":\"Veronica Mars\",\"release_date\":\"2014-03-14\",\"poster_path\":\"/nS3L07mQfcNJcisLEKgi8fWoBS1.jpg\",\"popularity\":48.7870515185193,\"title\":\"Veronica Mars\",\"vote_average\":7.2,\"vote_count\":19},{\"adult\":false,\"backdrop_path\":\"/iJtq3PHsLgjcYIrNlT2glzEdBo5.jpg\",\"id\":110415,\"original_title\":\"Snowpiercer\",\"release_date\":\"2013-08-01\",\"poster_path\":\"/3J4QoMpQYE2MehOTQG9X2KUP4aq.jpg\",\"popularity\":47.3945315956173,\"title\":\"Snowpiercer\",\"vote_average\":7.2,\"vote_count\":29},{\"adult\":false,\"backdrop_path\":\"/qMDiCjxfv6Y8JN2DFViTX5D1ORH.jpg\",\"id\":24253,\"original_title\":\"Flickan som lekte med elden\",\"release_date\":\"2009-09-18\",\"poster_path\":\"/qHRpU2d9NWB0WDulwgFwg6a9JRK.jpg\",\"popularity\":46.7216104479466,\"title\":\"The Girl Who Played with Fire\",\"vote_average\":6.8,\"vote_count\":141},{\"adult\":false,\"backdrop_path\":\"/1DfcGAQ4EVIZFnveo1IzHFtgFTS.jpg\",\"id\":175112,\"original_title\":\"The Pirate Fairy\",\"release_date\":\"2014-04-01\",\"poster_path\":\"/6VmPnBPDCTbpZ3Jj5lbgHD10IZm.jpg\",\"popularity\":46.03463240154,\"title\":\"The Pirate Fairy\",\"vote_average\":7.6,\"vote_count\":5},{\"adult\":false,\"backdrop_path\":\"/1RTiQXeHoEMXkZNWaB8W5uaEZ2.jpg\",\"id\":205220,\"original_title\":\"Philomena\",\"release_date\":\"2013-11-27\",\"poster_path\":\"/6BTXHupSPkrwsoz4Br6qwwSVmhj.jpg\",\"popularity\":45.1210557523094,\"title\":\"Philomena\",\"vote_average\":7.5,\"vote_count\":31},{\"adult\":false,\"backdrop_path\":\"/wRCPG1lsgfTFkWJ7G3eWgxCgv0C.jpg\",\"id\":101299,\"original_title\":\"The Hunger Games: Catching Fire\",\"release_date\":\"2013-11-22\",\"poster_path\":\"/tAhSyLxpaZJCr1oc2a3flvC2B7x.jpg\",\"popularity\":41.4568100606251,\"title\":\"The Hunger Games: Catching Fire\",\"vote_average\":7.7,\"vote_count\":518},{\"adult\":false,\"backdrop_path\":\"/hz3JfAikYXtaNWIJhWM4p5sy5OZ.jpg\",\"id\":49047,\"original_title\":\"Gravity\",\"release_date\":\"2013-10-04\",\"poster_path\":\"/2gPjLWIyrWlAn2DgKMOKTBnZYyO.jpg\",\"popularity\":40.0573419755177,\"title\":\"Gravity\",\"vote_average\":7.9,\"vote_count\":751},{\"adult\":false,\"backdrop_path\":\"/3FweBee0xZoY77uO1bhUOlQorNH.jpg\",\"id\":76338,\"original_title\":\"Thor: The Dark World\",\"release_date\":\"2013-11-08\",\"poster_path\":\"/aROh4ZwLfv9tmtOAsrnkYTbpujA.jpg\",\"popularity\":38.5511611536454,\"title\":\"Thor: The Dark World\",\"vote_average\":7.1,\"vote_count\":503},{\"adult\":false,\"backdrop_path\":\"/dNPmXYRS3nN4vD7MLtz5lP79DCB.jpg\",\"id\":11824,\"original_title\":\"Teen Wolf\",\"release_date\":\"1985-08-23\",\"poster_path\":\"/3TKJbKNpHvRP8YVnwbgfok41AAC.jpg\",\"popularity\":37.546317184227,\"title\":\"Teen Wolf\",\"vote_average\":7.1,\"vote_count\":31},{\"adult\":false,\"backdrop_path\":\"/kJzvjhJP6Xf7QQofVl3y0NvpwmI.jpg\",\"id\":256731,\"original_title\":\"Bad Country\",\"release_date\":\"2014-03-10\",\"poster_path\":\"/6bjfGrtUYmuZzFCia3TcvY0Kz1e.jpg\",\"popularity\":37.123709776744,\"title\":\"Bad Country\",\"vote_average\":5.5,\"vote_count\":3},{\"adult\":false,\"backdrop_path\":\"/6Ace8kIosYGnAiJUHgbLO4MNI6k.jpg\",\"id\":77067,\"original_title\":\"DeadHeads\",\"release_date\":\"2011-04-29\",\"poster_path\":\"/A7kD47MEXywqKPeKHrxBfkvPTqy.jpg\",\"popularity\":36.41,\"title\":\"DeadHeads\",\"vote_average\":5.8,\"vote_count\":6}],\"total_pages\":7848,\"total_results\":156953}";
uuid = "bb4cca64-b0c4-0308-b92f-49980466092c";
}
The code that i tried till now
[MYCloud callFunctionInBackground:#"popularMovie"
withParameters:#{#"movie": #" "}
block:^(id response, NSError *error) {
if (!error) {
// ratings is 4.5
NSArray *movieArray;
movieArray = [response allValues];
NSDictionary *firstObject = [movieArray objectAtIndex:0];
NSDictionary *entities = [firstObject objectForKey:#"results"];
NSLog(#"json :%#",[response description]);
it crashes at NSDictionary *entities = [firstObject objectForKey:#"results"];
======================================================================================
this is the crash log on trying
NSDictionary *blah = [NSJSONSerialization JSONObjectWithData:response options:0 error:nil];
2014-03-29 16:21:48.572 Moviez[6804:60b] -[__NSDictionaryM bytes]: unrecognized selector sent to instance 0x9daf000
2014-03-29 16:21:48.575 Moviez[6804:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM bytes]: unrecognized selector sent to instance 0x9daf000'
* First throw call stack:
(
0 CoreFoundation 0x026271e4 exceptionPreprocess + 180
1 libobjc.A.dylib 0x023a68e5 objc_exception_throw + 44
2 CoreFoundation 0x026c4243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0261750b __forwarding + 1019
4 CoreFoundation 0x026170ee _CF_forwarding_prep_0 + 14
5 Foundation 0x020ec4bc -[_NSJSONReader findEncodingFromData:withBOMSkipLength:] + 36
6 Foundation 0x020ec66b -[_NSJSONReader parseData:options:] + 63
7 Foundation 0x020ecc30 +[NSJSONSerialization JSONObjectWithData:options:error:] + 161
8 Moviez 0x00002884 32-[HILViewController viewDidLoad]_block_invoke + 212
9 Moviez 0x00053f47 __40-[PFTask thenCallBackOnMainThreadAsync:]_block_invoke_2 + 241
10 libdispatch.dylib 0x02c7a7b8 _dispatch_call_block_and_release + 15
11 libdispatch.dylib 0x02c8f4d0 _dispatch_client_callout + 14
12 libdispatch.dylib 0x02c7d726 _dispatch_main_queue_callback_4CF + 340
13 CoreFoundation 0x0268c43e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 14
14 CoreFoundation 0x025cd5cb __CFRunLoopRun + 1963
15 CoreFoundation 0x025cc9d3 CFRunLoopRunSpecific + 467
16 CoreFoundation 0x025cc7eb CFRunLoopRunInMode + 123
17 GraphicsServices 0x0420b5ee GSEventRunModal + 192
18 GraphicsServices 0x0420b42b GSEventRun + 104
19 UIKit 0x01066f9b UIApplicationMain + 1225
20 Moviez 0x00002ded main + 141
21 libdyld.dylib 0x02ec4701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
UPDATED to match the full data you are getting
As you say response is already an NSDictionary so your code should be something like this...
// Results is an Array of movie entries
NSDictionary *movieData = [response objectForKey:#"data"];
NSArray * movieArray = [movieData objectForKey:#"results"];
NSDictionary *firstObject = [movieArray objectAtIndex:0];
Or in modern objective c you could do
NSDictionary *movieData = response[#"data"];
NSArray * movieArray = movieData[#"results"];
NSDictionary *firstObject = movieArray[0];
Or if you are really confident its always going to contain something you could do
NSDictionary *firstObject = response[#"data"][#"results"][0];
(I think)
Well the top level object is an NSDictionary.
So...
NSDictionary *blah = [NSJSONSerialization JSONObjectWithData:theData options:0 error:nil];
That will turn the data into an object that you can use.

Lua accessing table stored in external file

I have an external lua file that has a table stored in it that is formatted as follows:
sgeT = {
2535047 = {
{
["account"] = "TG-MCB110105",
["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
["execEpoch"] = 1305825864,
["execModify"] = "Thu May 19 12:24:24 2011",
["execType"] = "user:binary",
["jobID"] = "2535047",
["numCores"] = "128",
["numNodes"] = "8",
pkgT = {
},
["runTime"] = "65125",
["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
sizeT = {
["bss"] = "104552",
["data"] = "192168",
["text"] = "10650813",
},
["startEpoch"] = "1335843433",
["startTime"] = "Mon Apr 30 22:37:13 2012",
["user"] = "guo",
},
},
2535094 = {
{
["account"] = "TG-MCB110105",
["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
["execEpoch"] = 1305825864,
["execModify"] = "Thu May 19 12:24:24 2011",
["execType"] = "user:binary",
["jobID"] = "2535094",
["numCores"] = "128",
["numNodes"] = "8",
pkgT = {
},
["runTime"] = "81635",
["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
sizeT = {
["bss"] = "104552",
["data"] = "192168",
["text"] = "10650813",
},
["startEpoch"] = "1335823028",
["startTime"] = "Mon Apr 30 16:57:08 2012",
["user"] = "guo",
},
}
I want to iterate through the table like an array and return the exec key, value pair, and I am completely new to lua and I am using the following script:
FileStr = "lariatData-sgeT-2012-05-31.lua"
Hnd, ErrStd = io.open(FileStr, "r")
myTable = loadTable(FileStr)
if Hnd then
for Str in Hnd:lines() do
print(Str, "\n")
for exec, val in pairs(myTable) do
print(exec.." "..val, "\n")
end
end
Hnd.close()
else
print(ErrStr, "\n")
end
However, it is returning that the table is nil. What am I doing wrong?
In continuation of comments above:
-- Notice that I've used `[2535047]`
sgeT = {
[2535047] = {
{
["account"] = "TG-MCB110105",
["exec"] = "/share/home/00288/tg455591/NAMD_2.8b3/NAMD_2.8b3_Linux-x86_64-MVAPICH-Intel-Ranger/namd2",
["execEpoch"] = 1305825864,
["execModify"] = "Thu May 19 12:24:24 2011",
["execType"] = "user:binary",
["jobID"] = "2535047",
["numCores"] = "128",
["numNodes"] = "8",
pkgT = {
},
["runTime"] = "65125",
["sha1"] = "e157dd510a7be4d775d6ceb271373ea24e7f9559",
sizeT = {
["bss"] = "104552",
["data"] = "192168",
["text"] = "10650813",
},
["startEpoch"] = "1335843433",
["startTime"] = "Mon Apr 30 22:37:13 2012",
["user"] = "guo",
},
},
}
The above is your file. Then, your Lua program shall be:
FileStr = "lariatData-sgeT-2012-05-31.lua"
Hnd, ErrStr = io.open(FileStr, "r")
if Hnd then
dofile(FileStr)
for Str in Hnd:lines() do
print(Str, "\n")
for exec, val in pairs(sgeT) do
print(exec.." "..val, "\n")
end
end
Hnd.close()
else
print(ErrStr, "\n")
end

Resources