After parsing a XML file the output is coming in following format in a dictionary
Now I want to convert it into array of dictionaries
Can anyone suggest a proper solution
Printing description of rootDictionary:
<CFBasicHash 0x8866000 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
0 : <CFString 0x8869490 [0x1078b38]>{contents = "Data"} = <CFBasicHash 0x88695c0 [0x1078b38]>{type = mutable dict, count = 1,
entries =>
0 : <CFString 0x8869610 [0x1078b38]>{contents = "row"} = (
{
Address = {
text = "Skt. Clemens Torv 2-6";
};
City = {
text = "Aarhus C";
};
Country = {
text = Danmark;
};
Description = {
text = Ottopiste;
};
Lat = {
text = "56.156818";
};
Lon = {
text = "10.2092532";
};
Name = {
text = Ottopisteet;
};
Type = {
text = 1;
};
Zip = {
text = 8000;
};
},
{
Address = {
text = "Kauppatie 66";
};
City = {
text = Kauhava;
};
Country = {
text = Finland;
};
Description = {
text = "(null)";
};
Lat = {
text = "63.1001586";
};
Lon = {
text = "23.0463634";
};
Name = {
text = I m Here;
};
Type = {
text = 2;
};
Zip = {
text = 62200;
};
}
)
}
}
Now the output dictionary is in above format now again I want to convert it into array of dictionaries
Actual format of XML for reference:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Data>
<row>
<Lat>56.156818</Lat>
<Lon>10.2092532</Lon>
<City>Aarhus C</City>
<Address>Skt. Clemens Torv 2-6</Address>
<Zip>8000</Zip>
<Country>Danmark</Country>
<Name>Ottopisteet</Name>
<Description>Ottopiste</Description>
<Type>1</Type>
</row>
<row>
<Lat>63.1001586</Lat>
<Lon>23.0463634</Lon>
<City>Kauhava</City>
<Address>Kauppatie 66</Address>
<Zip>62200</Zip>
<Country>Finland</Country>
<Name>I am Here</Name>
<Description>(null)</Description>
<Type>2</Type>
</row>
</Data>
DO this:
NSMutableArray *arrData = [[parsedValueDictionary objectForKey:#"Data"] objectForKey:#"row"];
You could just iterate on the dictionary of rows and add each object to an array:
NSMutableArray *arrayOfDictionaries = [[NSMutableArray alloc] init];
NSDictionary *rows = [rootDictionary objectForKey:#"Data"];
[rows enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
[arrayOfDictionaries addObject:obj];
}];
UPDATE: I'm assuming that there are just "row" entries in the "Data" dictionary. Otherwise you should perform a check on the key to si if it's a "row" before.
Related
I have one nsmutable Array , How to add one keyvalue in nsmutable array at index number
This is array
(
{
id = 1;
name = "Tommy";
},
{
id = 2;
name = "Dad";
},
{
id = 3;
name = "Mom";
},
{
id = 4;
name = "Vic";
},
{
id = 5;
name = "Tom";
},
{
id = 6;
name = "Pam";
}
)
Now Index number 3 how to add "Class = 4th" and change name
{
id = 4;
name = "Nishant ";
class = "4th"
}
Please give ME solution
i am new in Swift.
When I'm getting request from google maps API i use this method to get json:
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options: NSJSONReadingAllowFragments error: nil];
jsons look's like this one:
{
routes = (
{
bounds = {
northeast = {
lat = "50.90961";
lng = "20.6492";
};
southwest = {
lat = "50.05775000000001";
lng = "19.94544";
};
};
copyrights = "Dane do Mapy \U00a92012 Google";
legs = (
{
distance = {
text = "126 km";
value = 126118;
};
duration = {
text = "2 godz. 0 min";
value = 7229;
};
"end_address" = "Aleja Powstania Warszawskiego 15, 30-001 Krak\U00f3w, Polska";
"end_location" = {
lat = "50.06";
lng = "19.9589";
};
"start_address" = "Mas\U0142owska 2, 25-001 Kielce, Polska";
"start_location" = {
lat = "50.88884";
lng = "20.6492";
};
steps = (
{
distance = {
text = "0,1 km";
value = 109;
};
duration = {
text = "1 min";
value = 39;
};
"end_location" = {
lat = "50.88824";
lng = "20.64796";
};
"html_instructions" = "Kieruj si\U0119 <b>Mas\U0142owska</b> na <b>po\U0142udniowy zach\U00f3d</b> w stron\U0119 <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
polyline = {
points = "gfruHo``}BvBvF";
};
"start_location" = {
lat = "50.88884";
lng = "20.6492";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,2 km";
value = 221;
};
duration = {
text = "2 min";
value = 97;
};
"end_location" = {
lat = "50.88896";
lng = "20.64503";
};
"html_instructions" = "Skr\U0119\U0107 <b>w prawo</b> w <b>Szyd\U0142\U00f3wek G\U00f3rny</b>";
polyline = {
points = "obruHwx_}BaCpOMv#";
};
"start_location" = {
lat = "50.88824";
lng = "20.64796";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,6 km";
value = 596;
};
duration = {
text = "1 min";
value = 56;
};
"end_location" = {
lat = "50.88427";
lng = "20.64091";
};
"html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>Warszawska</b>";
polyline = {
points = "_gruHmf_}BrGvFjQ`OhA|#";
};
"start_location" = {
lat = "50.88896";
lng = "20.64503";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "1,0 km";
value = 950;
};
duration = {
text = "2 min";
value = 104;
};
"end_location" = {
lat = "50.88779";
lng = "20.62863";
};
"html_instructions" = "Skr\U0119\U0107 w drug\U0105 <b>w prawo</b> w kierunku <b>Jesionowa</b>";
polyline = {
points = "uiquHul~|BiEvScCzKs#xCmBhK_#dD}#dIk#tBYr#Mf#Op#MnA";
};
"start_location" = {
lat = "50.88427";
lng = "20.64091";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "4,2 km";
value = 4185;
};
duration = {
text = "5 min";
value = 292;
};
"end_location" = {
lat = "50.90806000000001";
lng = "20.58021";
};
"html_instructions" = "Na rondzie zjazd nr <b>1</b> w <b>\U0141\U00f3dzka</b>";
polyline = {
points = "u_ruH}_||BCd#Cf#?N#RDPHJJHsBzQIl#OrAm#bEEV[vBa#fBUfA_A~Bk#jA}DvGyDpEu#fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba#fCQjA]tBi#hC}#zDsB|GeBdDwD|Gg#~#cAhBGJ_H`Mo#jAiB`DaAlBcBfD_#`A[fACDuCrMcC`L?#{AhHw#rDiDhPSx#e#dBc#vAa#fAi#jAgCfF{#rAe#p#A#m#p#kBhB";
};
"start_location" = {
lat = "50.88779";
lng = "20.62863";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "0,2 km";
value = 213;
};
duration = {
text = "1 min";
value = 54;
};
"end_location" = {
lat = "50.90968";
lng = "20.57859";
};
"html_instructions" = "Kontynuuj wzd\U0142u\U017c <b>Trasa 74</b>";
polyline = {
points = "k~uuHiqr|BkBzAiEtEMP";
};
"start_location" = {
lat = "50.90806000000001";
lng = "20.58021";
};
"travel_mode" = DRIVING;
},
{
distance = {
text = "92,8 km";
value = 92776;
};
duration = {
text = "1 godz. 17 min";
value = 4641;
};
"end_location" = {
lat = "50.24777";
lng = "20.08501";
};
"html_instructions" = "Skr\U0119\U0107 <b>w lewo</b> w <b>E77</b>";
......
status = OK;
}
Next i use this method to get value for "points" key:
NSArray *items = [jsonArray valueForKeyPath:#"routes.legs.steps.polyline.points"];
When i want to print arrays elements it give only one element (it's strange because i have 25 points in json), and result looks like this one:
(
(
"gfruHo``}BvBvF",
"obruHwx_}BaCpOMv#",
"_gruHmf_}BrGvFjQ`OhA|#",
"uiquHul~|BiEvScCzKs#xCmBhK_#dD}#dIk#tBYr#Mf#Op#MnA",
"u_ruH}_||BCd#Cf#?N#RDPHJJHsBzQIl#OrAm#bEEV[vBa#fBUfA_A~Bk#jA}DvGyDpEu#fAsA`C_AhBeAlCmAhDgAnDuAvEwAzFyAvGgA|F]bDH`Ba#fCQjA]tBi#hC}#zDsB|GeBdDwD|Gg#~#cAhBGJ_H`Mo#jAiB`DaAlBcBfD_#`A[fACDuCrMcC`L?#{AhHw#rDiDhPSx#e#dBc#vAa#fAi#jAgCfF{#rAe#p#A#m#p#kBhB",
"k~uuHiqr|BkBzAiEtEMP",
"ohvuHegr|Bl#rCN`#Xf#n#h#xAx#hAz#nAdCtFdf#~Dv]~Ipu#zHpq#hAjIpA`JhBbJ`CxJjC~IfDlK`AtCfAvCfCdGdGhKpB|CvD`F`D|D~BzBhFbF`DvCjDhC`DdBtFtC|EtBxSjIfAd#|ErBzB~#~InDtX`KjDzArBz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get a normal NSArray with 25 NSString object?
You're looking at the correct output from the polyline field in the JSON. The data is encoded with Google's polyline encoding, which is documented here
https://developers.google.com/maps/documentation/utilities/polylinealgorithm
There are a number of methods online for parsing said string in objective-c, such as this one.
Ok, i have found the solution parsing json with NSJSONSerialization:
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataRequest options:NSJSONReadingMutableContainers error: nil];
NSArray * items = [jsonArray valueForKeyPath:#"routes.legs.steps.polyline.points"];
items = [items objectAtIndex:0];
items = [items objectAtIndex:0];
And in items array you will get string with encode direction
I am trying to create UITableView from my NSDictionary, but how can I use objectForKey? Convert to NSArray?
I need help.
My dictionary looks like:
**
{
"allow_comments" = 1;
category = 1024194;
"created_at" = "2011-11-17T01:44";
forum = 1005401;
"forum_obj" = {
"created_at" = "2011-09-03 03:04:43.514514";
description = "";
id = 1005401;
name = smartfiction;
shortname = smartfiction;
};
hidden = 0;
id = 474530565;
identifier = (
"2507 http://smartfiction.ru/?p=2507"
);
"num_comments" = 2;
slug = "thread_9714";
title = "\U041e\U0440\U0438\U0444\U043b\U0430\U043c\U043c\U0430. \U042d\U0436\U0435\U043d \U0418\U043e\U043d\U0435\U0441\U043a\U043e";
url = "http://smartfiction.ru/prose/oriflamma/";
}
2011-11-19 10:21:25.992 ARSSReader[3067:15503] id 474530565
2011-11-19 10:21:26.864 ARSSReader[3067:15503] Ответ #2: (
{
author = {
avatar = {
cache = "http://www.gravatar.com/avatar.php?gravatar_id=af6c6f08e213427ad611b00589db00f9&size=32&default=http://mediacdn.disqus.com/1321567697/images/noavatar32.png";
permalink = "http://www.gravatar.com/avatar.php?gravatar_id=af6c6f08e213427ad611b00589db00f9&size=32&default=http://mediacdn.disqus.com/1321567697/images/noavatar32.png";
};
emailHash = af6c6f08e213427ad611b00589db00f9;
isAnonymous = 1;
name = "S Shv";
profileUrl = "http://disqus.com/guest/af6c6f08e213427ad611b00589db00f9/";
url = "";
};
createdAt = "2011-11-17T22:39:36";
dislikes = 0;
forum = smartfiction;
id = 367003621;
isApproved = 1;
isDeleted = 0;
isEdited = 0;
isFlagged = 0;
isHighlighted = 0;
isJuliaFlagged = 1;
isSpam = 0;
likes = 0;
media = (
);
message = "\U043f\U043e\U0440\U0430\U0437\U0438\U043b\U043e )";
parent = "<null>";
points = 0;
"raw_message" = "\U043f\U043e\U0440\U0430\U0437\U0438\U043b\U043e )";
thread = 474530565;
},
{
author = {
avatar = {
cache = "http://www.gravatar.com/avatar.php?gravatar_id=c1c53088e04a60aab74cd1f149117e69&size=32&default=http://mediacdn.disqus.com/1321567697/images/noavatar32.png";
permalink = "http://www.gravatar.com/avatar.php?gravatar_id=c1c53088e04a60aab74cd1f149117e69&size=32&default=http://mediacdn.disqus.com/1321567697/images/noavatar32.png";
};
emailHash = c1c53088e04a60aab74cd1f149117e69;
isAnonymous = 1;
name = "3,14";
profileUrl = "http://disqus.com/guest/c1c53088e04a60aab74cd1f149117e69/";
url = "";
};
createdAt = "2011-11-17T02:52:46";
dislikes = 0;
forum = smartfiction;
id = 365544459;
isApproved = 1;
isDeleted = 0;
isEdited = 0;
isFlagged = 0;
isHighlighted = 0;
isJuliaFlagged = 1;
isSpam = 0;
likes = 1;
media = (
);
message = "\U0427\U0438\U0442\U0430\U0442\U044c \U043d\U0430 \U043d\U043e\U0447\U044c \U043f\U0440\U043e\U043a\U0440\U0430\U0441\U0442\U0438\U043d\U0430\U0442\U043e\U0440\U0430\U043c. \U0412\U043f\U0440\U043e\U0447\U0435\U043c, \U043b\U0443\U0447\U0448\U0435 \U0443\U0442\U0440\U043e\U043c.";
parent = "<null>";
points = 1;
"raw_message" = "\U0427\U0438\U0442\U0430\U0442\U044c \U043d\U0430 \U043d\U043e\U0447\U044c \U043f\U0440\U043e\U043a\U0440\U0430\U0441\U0442\U0438\U043d\U0430\U0442\U043e\U0440\U0430\U043c. \U0412\U043f\U0440\U043e\U0447\U0435\U043c, \U043b\U0443\U0447\U0448\U0435 \U0443\U0442\U0440\U043e\U043c.";
thread = 474530565;
}
)
**
It is comments from disqus.com. I want to create dictionaries for all comments and use them to create UITableView cells.
Instead of storing the comments from disqus.com in a dictionary, store each comment in an entry inside an array (it looks like each comment has a block of text you can encapsulate into an object which you can store into an array).
Dictionaries are good for associating values with keys.
Arrays are great for storing lists of objects, much like a list of comments from a disqus site.
{
education = (
{
school = {
id = 108102169223234;
name = psss;
};
type = College;
year = {
id = 142833822398097;
name = 2010;
};
}
);
email = "amvijaycse#gmail.com";
"first_name" = Vijay;
gender = male;
id = 100000782204693;
"last_name" = Kumar;
link = "http://www.facebook.com/profile.php?id=100000782204693";
locale = "en_US";
location = {
id = 106377336067638;
name = "Bangalore, India";
};
name = "Vijay Kumar";
timezone = "5.5";
"updated_time" = "2010-11-21T07:45:11+0000";
}
i needed email,firstname,lastname.
[EDIT: Removed because it was inaccurate]
I have a dictionary/list balance_sign_dict from which I need to retrieve 2 values based on condition.
The source XML for this is;
<Value>
<Signature>-873</Signature>
<Amount>1501042000</Amount>
</Value>
I want to parse data from the dict shown below;
Could you please tell me how do I parse conditional data (e.g. if I want the value of "Amount" node for "Signature" node having -873) i.e. it should return me 1501042000
I want a generic way of parsing the values from the dictionary below.
Printing description of balance_sign_dict:
(
{
nodeChildArray = (
{
nodeContent = "-873";
nodeName = Signature;
},
{
nodeContent = 1501042000;
nodeName = Amount;
}
);
nodeName = Value;
},
{
nodeChildArray = (
{
nodeContent = "-1228";
nodeName = Signature;
},
{
nodeContent = 428586000;
nodeName = Amount;
}
);
nodeName = Value;
},
{
nodeChildArray = (
{
nodeContent = "-1140";
nodeName = Signature;
},
{
nodeContent = 79370000;
nodeName = Amount;
}
);
nodeName = Value;
},
Here is what I am trying now;
for (NSDictionary *valueNode in balance_sign_dict){
for (NSArray *nodeChildArray in [valueNode objectForKey:#"nodeChildArray"]){
NSString *tmpNodeName = [nodeChildArray objectAtIndex:1];
NSDictionary *tmpD = [valueNode objectAtIndex:1];
if ([tmpNodeName isEqualToString:#"Signature"]) {
tmpSignVal = [nodeChildArray objectAtIndex:0];
if ([tmpSignVal isEqualToString:#"-873"]) {
tmpSignAmt = [nodeChildArray objectAtIndex:1];
}
}
But for some reason the 2nd part of nodeChildArray gets removed...i.e. nodeContent = 1501042000;
nodeName = Amount;
I am not sure how to access that part.
Please help.
}
}
Actually here is what i get during alternate runs;
Printing description of nodeChildArray:
{type = mutable dict, count = 2,
entries =>
0 : {contents = "nodeName"} = {contents = "Signature"}
1 : {contents = "nodeContent"} = {contents = "-1507"}
}
Printing description of nodeChildArray:
{type = mutable dict, count = 2,
entries =>
0 : {contents = "nodeName"} = {contents = "Amount"}
1 : {contents = "nodeContent"} = {contents = "631000"}
}
How do I get the Amount value for matching Signature value?
NSArray and NSDictionary are the best container classes that I know of in any language that I am experienced with. NSDictionary can store any object or primitive, including an NSArray, and vice versa. When I say dictionary I am arbitrarily referring to either class.
When you output the description of any dictionary (which is as simple as NSLog(#"%#",myDict);, the entire contents of the dictionary are displayed like an XML (or maybe more accurately a JSON) document. It is easy to look at the content of the dictionary description to determine what is contained inside. () denotes an NSArray and {} denotes an NSDictionary.
// this is the beginning of an array.
(
// this is the beginning of index 0 of the array, an `NSDictionary`
{
// objectForKey:#"nodeChildArray"] isKindOfClass:[NSArray class]]
nodeChildArray = ( // inside nodeChildArray is another array
{ // index 0 of array is a dictionary with two keys
// objectForKey:#"nodeContent" == #"-873"
nodeContent = "-873";
// objectForKey:#"nodeName" == #"Signature"
nodeName = Signature;
}, // end of index 0
{ // index 1 of array
nodeContent = 1501042000;
nodeName = Amount;
} // end of index 1
); // end of nodeChildArray, an NSArray stored at objectForKey:#"nodeChildArray"
// this is the only other element in the dictionary, a string
// objectForKey:#"nodeName"] isKindOfClass:[NSString class]]
nodeName = Value;
}, // end of the dictionary containing nodeChildArray and nodeName
{ // objectAtIndex:1
nodeChildArray = (
{
nodeContent = "-1228";
nodeName = Signature;
},
{
nodeContent = 428586000;
nodeName = Amount;
}
);
nodeName = Value;
},
{ // objectAtIndex:2
nodeChildArray = (
{
nodeContent = "-1140";
nodeName = Signature;
},
{
nodeContent = 79370000;
nodeName = Amount;
}
);
nodeName = Value;
},
);
So you can see from looking at your NSDictionary debug output that it's actually an NSArray. The order of structures is NSArray->NSDictionary->NSArray->NSString <--> [[[[NSArray objectAtIndex:i] NSDictionary objectForKey:#"nodeChildArray"] NSArray objectAtIndex:j] NSDictionary objectForKey:#"nodeContent"], finally returning an NSString. Use this as the most recent code - in the last version of the code I passed balance_sign_dict as an NSDictionary. This might have even worked! But it is much better form to cast the argument properly. If you don't know whether or not your dictionary is going to start with an NSArray or an NSDictionary, you can cast it as an id and check its type with [theIdObject isKindOfClass:[{NSArray,NSDictionary} class]]. Don't use the {} and choose one of the two in your code.
-(NSDictionary*)parseBalance:(NSArray*)balance_sign_dict
{
NSMutableDictionary* theResults = [NSMutableDictionary dictionary];
for( NSDictionary* nodeDict in balance_sign_dict )
{
NSArray* nodeChildArray = [nodeDict objectForKey:#"nodeChildArray"];
[theResults addObject:[[nodeChildArray objectAtIndex:1] objectForKey:#"nodeContent"] forKey:[[nodeChildArray objectAtIndex:0] objectForKey:#"nodeContent"]];
}
return theResults;
}