IOS中本機存放區和查看json資料

來源:互聯網
上載者:User

標籤:

1.代碼建立json檔案,並儲存到本地第一步.設定json檔案的儲存路徑NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myJson.json"];   NSLog(@"%@",filePath); 第二步.準備儲存資料NSMutableArray *arr = [[NSMutableArray alloc]init]; //用來盛放資料的value   NSDictionary *dic = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"};NSDictionary *dic1 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"};NSDictionary *dic2 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"};   [arr addObjectsFromArray:@[dic,dic1,dic2]];      NSDictionary *json_dic = @{@"arr":arr};//key為arr value為arr數組的字典 第三步.封包資料NSData *json_data = [NSJSONSerialization dataWithJSONObject:json_dic options:NSJSONWritingPrettyPrinted error:nil];   第四步.寫入資料[json_data writeToFile:filePath atomically:YES]; 經過這四步,就在本地指定路徑filePath建立了一個json檔案。(根目錄是一個字典 key為arr value為arr數組的字典)  2.讀取本地json資料 NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myJson.json"];//擷取json檔案儲存的路徑   NSData *data = [NSData dataWithContentsOfFile:filePath];//擷取指定路徑的data檔案   id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //擷取到json檔案的跟資料(字典) NSArray *arr = [json objectForKey:@"arr”];//擷取指定key值的value,是一個數組   for (NSDictionary *dic in arr) {           NSLog(@"%@",[dic objectForKey:@"key1"]);//遍曆數組 }    

IOS中本機存放區和查看json資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.