简体   繁体   中英

How can I access one value among some values

{
"id"=12
"genres_en" = "thriller,movies,action";
}
{
"id"=13
"genres_en" = "thriller,horror";
}

Hi everyone , I have one json like this..I mean i have one content and all movie has their genre ..And i need to do one categorization ..For example I need to check which genre of movie is horror..Or which one is thriller..I m getting all value like this:

--> "horror,movies" But how can i do the controllation to check whether the genre of this movie includes horror or not..??what is your suggesstion?

Thank you

You could use componentsSeparatedByString

NSDictionary *dic = PARSE The JSON;

NSString *values = [dic objectForKey:"genres_en"];
NSString *firstValue = [[values componentsSeparatedByString:","] objectAtIndex:0];

I don't have Xcode nearby, so the following code may contain an error(or two :) ).

Try it like that:

NSDictionary *yourDict = //your parsed json
NSString *genres = [yourDict objectForKey:@"genres_en"];
if ([genres rangeOfString:@"horror"].location != NSNotFound) {
  return YES;
} else {
  return NO;
}

Hope it helps

 for(int j=0;j< GenresArray.count; j++)
                {  
                    NSString *value=[[genre componentsSeparatedByString: @","]objectAtIndex:j];

                    if([value isEqualToString:@"movies"])
                    {
                        [genreMovies addObject:value];
                    }
                }

Thank you for your help guys ..Now i have my result:)

You can try to check if the string contains the substring you want . Like this:

NSString *typesString = [dic objectForKey:"genres_en"];
NSRange range = [typesString rangeOfString:@"horror"];
if(range.location != NSNotFound)
{
    //it contains the substring "horror"
}

Or , the better way would be to get the array of types.

NSArray *types = [[typesString componentsSeparatedByString:","];

Now you can store this array for each entry and whenever you want to check if it belongs to a certain gender , just loop through the types array ( of each entry ) and compare the strings.

Hope this helps.

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM