2012-08-01 107 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; 

}