stirng 读取json文件 jsonkit 使用

[java]  view plain copy
  1. <pre name="code" class="java"><pre name="code" class="java">1.将字典或者数组反序列化为NSString.  
  2.     NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];  
  3.     NSMutableDictionary *alert = [NSMutableDictionary dictionary];  
  4.     NSMutableDictionary *aps = [NSMutableDictionary dictionary];  
  5.     [alert setObject:@"a msg come!" forKey:@"body"];  
  6.     [aps setObject:alert forKey:@"alert"];  
  7.     [aps setObject:@"3" forKey:@"bage" ];  
  8.     [aps setObject:@"def.mp3" forKey:@"sound"];  
  9.     [jsonDic setObject:aps forKey:@"aps"];  
  10.       
  11.     NSString *strJson = [jsonDic JSONString];  
  12.       
  13.     NSLog(@"%@",strJson);  
  14.       
  15.       
  16. //    2.将NSString反序列化为数组、字典.  
  17.     NSDictionary *result = [strJson  objectFromJSONString];  
  18. //    或者  
  19.     NSDictionary *result = [dataJson  objectFromJSONData];  
  20.       
  21.     NSLog(@"%@",result);</pre><br>  
  22. NSError *error;NSString *textFileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];if (textFileContents == nil) {NSLog(@"Error reading text file. %@", [error  
  23.  localizedFailureReason]);}NSDictionary *result = [textFileContents objectFromJSONString];NSLog(@"res= %@", [result objectForKey:@"cmd"]);  
  24. <pre></pre>  
  25. <br>  
  26. <br>  
  27. <pre></pre>  
  28. <p>读取jsonKIT文件,转化成json字典</p>  
  29. <p><br>  
  30. </p>  
  31.   
  32. </pre>  
JSON 文件读取并转换为字符串可采用以下方法: ### 使用 Java 自带类 - **使用 java.io 包中的类**:借助 FileReader、BufferedReader 等类逐行读取文件内容,再拼接成字符串。示例代码如下: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadJsonWithIo { public static String readJsonFile(String filePath) { StringBuilder jsonStr = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { jsonStr.append(line); } } catch (IOException e) { e.printStackTrace(); } return jsonStr.toString(); } public static void main(String[] args) { String filePath = "your_json_file.json"; String json = readJsonFile(filePath); System.out.println(json); } } ``` - **使用 java.nio 包中的类**:利用 Path、Files 等类一次性读取文件的所有字节,然后转换成字符串。示例代码如下: ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class ReadJsonWithNio { public static String readJsonFile(String filePath) { try { byte[] bytes = Files.readAllBytes(Paths.get(filePath)); return new String(bytes); } catch (IOException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String filePath = "your_json_file.json"; String json = readJsonFile(filePath); System.out.println(json); } } ``` ### 使用第三方库 - **使用 Google 的 Gson 库**:这些库不仅能将 JSON 文件读取为字符串,还可将 JSON 数据转换为 Java 对象或者反之。示例代码如下: ```java import com.google.gson.Gson; import java.io.FileReader; import java.io.IOException; public class ReadJsonWithGson { public static String readJsonFile(String filePath) { Gson gson = new Gson(); try (FileReader reader = new FileReader(filePath)) { Object jsonObj = gson.fromJson(reader, Object.class); return gson.toJson(jsonObj); } catch (IOException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String filePath = "your_json_file.json"; String json = readJsonFile(filePath); System.out.println(json); } } ``` - **使用 Jackson 库**:示例代码如下: ```java import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; public class ReadJsonWithJackson { public static String readJsonFile(String filePath) { ObjectMapper objectMapper = new ObjectMapper(); try { Object jsonObj = objectMapper.readValue(new File(filePath), Object.class); return objectMapper.writeValueAsString(jsonObj); } catch (IOException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String filePath = "your_json_file.json"; String json = readJsonFile(filePath); System.out.println(json); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值