2016-06-23 331 views
0

我有一個json看起來像這樣。從json中查找對象

{ 
    "countrycode" : 
    [["11","","91","INDIA"," New Delhi"], 
    ["112","Mahanagar Telephone Nigam Ltd (MTNL]","91","INDIA"," New Delhi"], 
    ["113","Reliance Communications Ltd","91","INDIA"," New Delhi"], 
    ["114","Bharti Airtel Ltd","91","INDIA"," New Delhi"], 
    ["115","Sistema Shyam TeleServices Ltd (MTS]","91","INDIA"," New Delhi"], 
    ["122","","91","INDIA"," Hapur Ghaziabad"], 
    ["1222","Bharat Sanchar Nigam Ltd (BSNL]","91","INDIA"," Hapur Ghaziabad"], 
    ["1223","Reliance Communications Ltd","91","INDIA"," Hapur Ghaziabad"]] 
} 

我想用我的價值找到第一行json的完美匹配記錄。我的價值可能是「1178920」或「1130123」或「1151593248」或「1223798」。

如果我的價值是1223798然後返回我:

["1223","Reliance Communications Ltd","91","INDIA"," Hapur Ghaziabad"]] 

我不能找到一種方法。請幫幫我。

+0

是你的json是否正確。因爲在你的json中只有第一個Array對象包含另一個數組檢查。 –

+1

請發佈您嘗試過的代碼,並描述它做了什麼錯誤 – Feldur

+0

@NiravDoctorwala我的json是正確的,請檢查http://jsonlint.com/進行驗證。 –

回答

-2

只要你想它得到的東西的方式:

Nsarray * object = [NSArray from countrycode] 

For (index = 0: index<object; index++) 
{ 
    NSArray * objectTest = object[index]; 

    NSString * arrValue = objectTest[0]; 

    if ([arrValue rangeOfString:@"YourValue"].location == NSNotFound) { 
     NSLog(@"string does not contain Value"); 
    } else { 
     NSLog(@"string contains Value!"); 
    } 

} 
2
NSPredicate *predicateString = [NSPredicate predicateWithFormat:@"ANY SELF BEGINSWITH %@", @"1223"]; 
NSArray *newarr = [NSMutableArray arrayWithArray:[[dict valueForKey:@"countrycode"] filteredArrayUsingPredicate:predicateString]]; 

希望這一個你的幫助。

+0

你的回答是正確的,但輸入像「1223」這樣的值時,它不會工作,而值將會「1223456712」 –

+0

@PramodTapaniya嘗試使用我的可編輯答案。 –

+0

您的解決方案也會返回記錄,例如「251223」。 bcoz字符串包含我們的價值。僅限prifix的強制性價值。 –