ios - How to get rid of multiple sets of parentheses in an array -
i'm using restkit object mapping json , i'm mapping array. json i'm mapping looks this: "parameters":[{"parameter_value":["smith"]}, {"parameter_value":[66211]}]
the array i'm getting looks this:
parameter value: ( ( smith ), ( 66211 ) )
when try convert array string via code: nsstring *nameidvalue = [[parametervaluearray valueforkey:@"description"] componentsjoinedbystring:@""];
string nameidvalue turns this:
( smith )( 66211 )
how rid of parentheses i'm left smith, 66211
you asked eliminating "sets of parentheses", given underlying structure series of nested collections (dictionaries , arrays), can achieve desired effect collapsing level or 2 structure. can kvc collection operator, @unionofarrays
, used in conjunction valueforkeypath
. if want string manipulation, can that, seems more logical write code renders simple array, , can use componentsjoinedbystring
if want simple string @ end.
what makes answer little complicated reported json doesn't match nslog
of resulting nsdictionary
, i'm unclear precisely input was. i'm going show 2 permutations of kvc collection operator, 1 matches provided sample json, , matches json inferred nsdictionary
structure reported.
if json is:
{"parameters":[{"parameter_value":["smith"]}, {"parameter_value":[66211]}]}
you parse follows (i'm using nsjsonreadingmutablecontainers
can change results; if you're not trying change array, rather extract appropriate results, don't need make mutable; it's you):
nsmutabledictionary *dictionary = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers error:&error];
that yield nsmutabledictionary
structure be:
{ parameters = ( { "parameter_value" = ( smith ); }, { "parameter_value" = ( 66211 ); } ); }
but replace parameters
with:
dictionary[@"parameters"] = [dictionary[@"parameters"] valueforkeypath:@"@unionofarrays.parameter_value"];
yielding:
{ parameters = ( smith, 66211 ); }
but, if had json like:
{"parameters":[["smith"],[66211]]}
and if parsed same nsjsonserialization
above, yield dictionary like:
{ parameters = ( ( smith ), ( 66211 ) ); }
and replace parameters value using kvc collection operator @unionofarrays
again, time using self
:
dictionary[@"parameters"] = [dictionary[@"parameters"] valueforkeypath:@"@unionofarrays.self"];
that yield:
{ parameters = ( smith, 66211 ); }
you asked:
how rid of parentheses i'm left smith, 66211
if you, literally, want @"smith, 66211"
, take simplified array , use componentsjoinedbystring
:
nsstring *string = [dictionary[@"parameters"] componentsjoinedbystring:@", "];
Comments
Post a Comment