2012-08-01 111 views
0

的名單我需要解析存儲在一個單一文件的JSON列表!解析JSON

我做了什麼至今, test.json文件包含:

{"location":"lille","lat":28.4,"long":51.7,"country":"FR"} 

這個文件我有以下

public class JsonReader { 
     public static void main(String[] args) throws ParseException { 
      JSONParser parser = new JSONParser(); 
    try { 
    Object obj = parser.parse(new FileReader("c:\\test.json")); 
    JSONObject locationjson= (JSONObject) obj; 
     String location= (String) locationjson.get("location"); 
     System.out.printf("%s",location); 
    long lat = (Long) locationjson.get("lat"); 
    System.out.printf("\t%d",lat); 
//similarly for other objects   

此代碼是一個工作的代碼,我能夠在文件test.json中只打印一個json

現在,如果我必須打印文件中的json列表:test1.json,如下所示:每行是一個有效的json,並且有單個文件中的json列表。我需要的是解析每個JSON並在每行中打印它。將使用一個bean類工作?

{"Atlas":{"location":"lille","lat":28.4,"long":51.7,"country":"FR"}} 
{"Atlas":{"location":"luxum","lat":24.1,"long":54.7,"country":"LU"}} 
{"Atlas":{"location":"ghent","lat":28.1,"long":50.1,"country":"BE"}} 
{"Atlas":{"location":"alborg","lat":23.4,"long":53.7,"country":"DN"}} 

您的幫助表示感謝!

+0

一個有效的JSON文件必須包含一個根。所以要麼你把你的元素放在一個列表中,要麼將它們包裝在一個容器元素中。 – 2012-08-01 07:52:55

+0

可能的重複線程 - http://stackoverflow.com/questions/11700482/convert-text-file-to-json-in-java http://stackoverflow.com/questions/11698447/json-parsing-through-multiple-對象http://stackoverflow.com/questions/11457856/json-read-file-parse-each-line-of-json-into-bean – adatapost 2012-08-01 07:56:10

回答

1

的JSON應該有一個根節點。

如果你沒有,你可以從文件一行一行地讀,並且每行傳遞到JSONParser包裹在StringReader(因爲JSONParser.parse()方法採用Reader)。

例如

BufferedReader in 
     = new BufferedReader(new FileReader("test.json")); 
    while(!done) { 
     String s = in.readLine(); 
     if (s == null) { 
     done = true; 
     } 
     else { 
     StringReader sr = new StringReader(s); 
     // etc... 
     } 
    } 

編輯:我假定您正在使用JSONParser。如果您使用的是不同的解析器(哪一個?),那麼它可能採取一個String說法。

+0

是的,我已經使用JSONParser ...但我如何通過讀取文件行並將每行傳入JSONParser? – labbyfrost 2012-08-01 08:12:05

+0

@labbyfrost - 見上 – 2012-08-01 10:52:37

0

JSONParser.parse()也需要字符串作爲aurgument。

與閱讀的FileReader併爲每個你閱讀使用JSONParser.parse(String)方法行的文件。

0

首先:確保你已經爲每個在頭文件中具有字符串屬性並在實現文件中合成的行創建了一個子類。然後在實現文件中創建一個屬性數組,是做parsing.In解析file..use NSJSONSerialization在您檢索數據的方法:

-(void)retrieveData{ 

    NSURL * url =[NSURL URLWithString:getDataURL]; 
    NSData *data =[NSData dataWithContentsOfURL:url]; 

    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    newsArray = [[NSMutableArray alloc]init]; 

    for (int i = 0; i < json.count; i++){ 
     { 
      NSString * cID =[[json objectAtIndex:i]objectForKey:@"Name1"]; 
      NSString * cName = [[json objectAtIndex:i]objectForKey:@"name2"]; 
      NSString * cState =[[json objectAtIndex:i]objectForKey:@"name3"]; 
      NSString * cPopulation =[[json objectAtIndex:i]objectForKey:@"Edition"]; 
      NSString * cCountry =[[json objectAtIndex:i]objectForKey:@"name4"]; 
City *myCity = [[City alloc]initWithCityID:cID andCityName:cName andCityState:cState andCityPopulation:cPopulation andCityCountry:cCountry]; 
      [newsArray addObject:myCity]; 
     } 
     [self.myTableView reloadData]; 
    }  
} 

然後檢索子對象或數組,並在名稱1名進入其中2節他們解析到你的表。另外請確保您已經設置了您的表格併爲其分配了一個單元格標識符並將其索引爲行。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    //static NSString *[email protected]"identity"; 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if(cell == nil){ 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 
    // cell.textLabel.text = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

    City *currentCity =[newsArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = currentCity.Name1; 
    cell.detailTextLabel.text = currentCity.Name2; 

    return cell; 

}