将平面文件转换为Java中的JSON

huangapple go评论60阅读模式
英文:

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(&quot;src/main/resources/flatfileex.txt&quot;));
    JSONObject jsonObject = (JSONObject) parser.parse(new InputStreamReader(new FileInputStream(&quot;src/main/resources/flatfileex.txt&quot;)));;
    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:

&lt;dependency&gt;
	&lt;groupId&gt;com.github.wnameless&lt;/groupId&gt;
	&lt;artifactId&gt;json-flattener&lt;/artifactId&gt;
	&lt;version&gt;0.1.0&lt;/version&gt;
&lt;/dependency&gt;

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(&quot;content.txt&quot;)){
    prop.load(inputStream);
    String contentAsJson = new ObjectMapper().writeValueAsString(prop);
    System.out.print(contentAsJson);
} catch (IOException exception) {
    System.out.println(&quot;Something went wrong!&quot;);
}

If your file contains

customerInfo.firstName=abc
customerInfo.lastName=aaa
customerInfo.nickNames.0.name=bbb
customerInfo.nickNames.0.meaning=ccc

It will print

{&quot;customerInfo.lastName&quot;:&quot;aaa&quot;,&quot;customerInfo.firstName&quot;:&quot;abc&quot;,&quot;customerInfo.nickNames.0.name&quot;:&quot;bbb&quot;,&quot;customerInfo.nickNames.0.meaning&quot;:&quot;ccc&quot;}

I guess that from here you can use json-flattener.

huangapple
  • 本文由 发表于 2020年10月12日 18:03:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64315662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定