2015-08-27 258 views
-4

我想只拿城市和國家的員工地址的對象值,這裏是JSON過濾JSON對象值

[ 
    { 
     "VacancyTitle": "PHP Engineer", 
     "VacancyJobType": "Full Time", 
     "EmployerCompanyName": "Vodafone", 
     "EmployerCompanyLogo": "d022813eadb769c3a972221621552d04.jpg", 
     "EmployerAddress": "10th Floor, Aitken Spence Tower 2, 315, Vauxhall Street, Colombo 02, Sri Lanka.ty", 
     "UserID": "144", 
     "block_count": "0" 
    } 
] 

我只想價值「的科倫坡02,斯里蘭卡」哪有我這樣做?

+3

嘗試使用一個string.Split方法,然後訪問返回的數組和ge t從那裏的值 – DieVeenman

回答

0
let s="10th Floor, Aitken Spence Tower 2, 315, Vauxhall Street, Colombo 02, Sri Lanka.ty" 
     let v = s.componentsSeparatedByString(",") 
     let stringYouNeed = v[v.count-2]+","+v[v.count-1] 
     print(stringYouNeed) 

要獲得JSON對象,你可以使用SwiftyJSON

0
 //parse the json data 
     NSError *error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                  options:kNilOptions 
                   error:&error];//you will get response data from server url or local json 

     NSArray *addressValue = [json valueForKey:@"EmployerAddress"]; 

     NSString* cityName = [[[addressValue componentsJoinedByString:@","] componentsSeparatedByString:@","] objectAtIndex:4]; 
     NSString* countryName = [[[addressValue componentsJoinedByString:@","] componentsSeparatedByString:@","] objectAtIndex:5]; 

     //here is your output 
     NSLog(@"City name = %@", cityName);//Colombo 02 
     NSLog(@"Country name = %@", countryName);//Sri Lanka.ty 
1
在Swfit 2.0的Xcode

7測試版5.你可以做到以下幾點:

let array = [["VacancyTitle": "PHP Engineer","VacancyJobType": "Full Time", 
"EmployerCompanyName": "Vodafone", 
"EmployerCompanyLogo": "d022813eadb769c3a972221621552d04.jpg", 
"EmployerAddress": "10th Floor, Aitken Spence Tower 2, 315, Vauxhall Street, Colombo 02, Sri Lanka.ty", 
"UserID": "144", 
"block_count": "0" 
]] 

let mapResult = array.map { $0["EmployerAddress"] } 

let employerAddress = mapResult[0]! 

let components = employerAddress.characters.split{$0 == ","}.map{String($0)} 

let count = components.count 
let finalString = components[count - 2] + ", " + components[count - 1] //" Colombo 02, Sri Lanka.ty" 

的Xcode 6.4雨燕1.2:

let array = [["VacancyTitle": "PHP Engineer","VacancyJobType": "Full Time", 
"EmployerCompanyName": "Vodafone", 
"EmployerCompanyLogo": "d022813eadb769c3a972221621552d04.jpg", 
"EmployerAddress": "10th Floor, Aitken Spence Tower 2, 315, Vauxhall Street, Colombo 02, Sri Lanka.ty", 
"UserID": "144", 
"block_count": "0" 
]] 

let mapResult = array.map { $0["EmployerAddress"] } 

let employerAddress = mapResult[0]! 

let components = split(employerAddress) {$0 == ","} 
let count = components.count 
let finalString = (components[count - 2] + ", " + components[count - 1])