英文:
Converting flatfile to JSON in Java
问题
以下是翻译好的部分:
我需要在Java中将一个扁平文件转换为JSON文件。
我的扁平文件内容如下:
customerInfo.firstName=abc
customerInfo.lastName=aaa
customerInfo.nickNames.0.name=bbb
customerInfo.nickNames.0.meaning=ccc
以下是我正在使用的代码片段:
JSONParser parser = new JSONParser();
Object obj = null;
try {
//obj = parser.parse(new FileReader("src/main/resources/flatfileex.txt"));
JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader(new FileInputStream("src/main/resources/flatfileex.txt")));
String flattenedJson = JsonFlattener.flatten(jsonObject.toJSONString());
System.out.println(flattenedJson);
} catch (FileNotFoundException | IOException | ParseException e) {
e.printStackTrace();
}
我在上面的代码中使用了jsonflattener库依赖:
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.1.0</version>
</dependency>
在解析文件时遇到错误,错误消息为“在位置0处出现意外字符[c]”。如何解决这个问题?
英文:
I need to convert a flat file to a JSON file in Java.
My flat file looks like this:
customerInfo.firstName=abc
customerInfo.lastName=aaa
customerInfo.nickNames.0.name=bbb
customerInfo.nickNames.0.meaning=ccc
Here is the piece of code I am using:
JSONParser parser = new JSONParser();
Object obj = null;
try {
//obj = parser.parse(new FileReader("src/main/resources/flatfileex.txt"));
JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader(new FileInputStream("src/main/resources/flatfileex.txt")));;
String flattenedJson = JsonFlattener.flatten(jsonObject.toJSONString());
System.out.println(flattenedJson);
} catch (FileNotFoundException | IOException | ParseException e) {
e.printStackTrace();
}
I am using jsonflattener dependency for the above code:
<dependency>
<groupId>com.github.wnameless</groupId>
<artifactId>json-flattener</artifactId>
<version>0.1.0</version>
</dependency>
Getting error while parsing the file Unexcexpected character [c] at position 0. How to resolve this?
答案1
得分: 2
以下是您要求的翻译内容:
你的输入文件看起来像是一个Java属性文件,因此你可以尝试这样做:
Properties prop = new Properties();
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("content.txt")){
prop.load(inputStream);
String contentAsJson = new ObjectMapper().writeValueAsString(prop);
System.out.print(contentAsJson);
} catch (IOException exception) {
System.out.println("Something went wrong!");
}
如果你的文件包含了以下内容:
customerInfo.firstName=abc
customerInfo.lastName=aaa
customerInfo.nickNames.0.name=bbb
customerInfo.nickNames.0.meaning=ccc
它将会输出:
{"customerInfo.lastName":"aaa","customerInfo.firstName":"abc","customerInfo.nickNames.0.name":"bbb","customerInfo.nickNames.0.meaning":"ccc"}
我猜从这里你可以使用 json-flattener
。
英文:
Your input file looks like a Java properties file, so you could try this:
Properties prop = new Properties();
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("content.txt")){
prop.load(inputStream);
String contentAsJson = new ObjectMapper().writeValueAsString(prop);
System.out.print(contentAsJson);
} catch (IOException exception) {
System.out.println("Something went wrong!");
}
If your file contains
customerInfo.firstName=abc
customerInfo.lastName=aaa
customerInfo.nickNames.0.name=bbb
customerInfo.nickNames.0.meaning=ccc
It will print
{"customerInfo.lastName":"aaa","customerInfo.firstName":"abc","customerInfo.nickNames.0.name":"bbb","customerInfo.nickNames.0.meaning":"ccc"}
I guess that from here you can use json-flattener
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论