简体   繁体   English

如何访问某些值中的一个值

[英]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: 大家好,我有一个这样的json ..我的意思是我只有一种内容,所有电影都有其类型..我需要进行一种分类..例如,我需要检查哪种类型的电影是恐怖的..一个是惊悚片..我正在这样获得所有价值:

--> "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 您可以使用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 :) ). 我附近没有Xcode,因此以下代码可能包含错误(或两个:))。

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. 现在,您可以为每个条目存储此数组,并且每当要检查它是否属于某个性别时,只需遍历(每个条目的)types数组并比较字符串即可。

Hope this helps. 希望这可以帮助。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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