0

単一のファイルに保存されている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に1つの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"}}

あなたの助けに感謝します!

4

3 に答える 3

1

JSON にはルート ノードが必要です。

それがない場合は、ファイルから行ごとに読み取り、各行を a でJSONParserラップされたに渡すことができますStringReader(JSONParser.parse()メソッドが a を取るため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は、引数を取ることができます。

于 2012-08-01T07:55:00.880 に答える
0

まず第一に、ヘッダーファイルに文字列プロパティを持ち、実装ファイルに合成された各行の息子項目のためだけにクラスを作成したことを確認してください。次に、解析を行う実装ファイルにプロパティ配列を作成します解析ファイル内で、データ取得メソッドで 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 *strIdentifier=@"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;

}
于 2015-07-23T01:29:36.023 に答える
0

JSONParser.parse()また、引数として文字列を取ります。

FileReader を使用してファイルを読み取り、読み取った行ごとにJSONParser.parse(String)メソッドを使用します。

于 2012-08-01T08:00:41.400 に答える