将 Java 属性文件转换为 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18507067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Java properties file into JSON string
提问by Aneesh
I'm writing a simple parser to convert a java properties file which contains entries in name=valuepair to a jsonstring.
Below is the code. The code requires that each entry is in a new line:
我正在编写一个简单的解析器来将包含name=value成对条目的 java 属性文件转换为json字符串。下面是代码。代码要求每个条目都在一个新行中:
sCurrentLine = br.readLine();
while ((sCurrentLine) != null)
{
config+=sCurrentLine.replace('=', ':');
sCurrentLine = br.readLine()
if(sCurrentLine!=null)
config+=",";
}
config+="}";
The function works fine except in cases when there are extra empty new lines in the properties file. (Ex: Suppose I write the last entry in the props file and then hit two enters, the file will contain two empty new lines after the last entry) . While the expected output is {name1:value1,name2:value2}, in the above case when extra new lines are present , I get the output as {name1:value1,name2:value2,}. The number of trailing ,increases with the number of empty new lines.
该函数工作正常,除非属性文件中有额外的空新行。(例如:假设我在 props 文件中写入最后一个条目,然后按两次输入,该文件将在最后一个条目之后包含两个空的新行)。虽然预期的输出是{name1:value1,name2:value2},但在上述情况下,当存在额外的新行时,我得到的输出为{name1:value1,name2:value2,}. 尾随,的数量随着空新行的数量而增加。
I know its because the readLine()reads the empty line while logically it shouldn't but how do I change this?
我知道它是因为readLine()读取空行而逻辑上它不应该但是我该如何改变它?
采纳答案by christopher
This could be solved using the contains()method. Simply ensure the existence of a "="in your line..
这可以使用该contains()方法解决。只需确保"="您的行中存在 a ..
while ((sCurrentLine) != null)
{
if(sCurrentLine.contains("=") {
config+=sCurrentLine.replace('=', ':');
sCurrentLine = br.readLine()
if(sCurrentLine!=null)
config+=",";
}
}
Example
例子
sCurrentLine = "name=dave"
if(sCurrentLine.contains("=")) // Evaluates to true.
// Do logic.
sCurrentLine = ""
if(sCurrentLine.contains("=")) // Evaluates to false.
// Don't do logic.
sCurrentLine = "\n"
if(sCurrentLine.contains("=")) // Evaluates to false.
// Don't do logic.
I know its because the readLine() reads the empty line while logically it shouldn't but how do I change this?
我知道它是因为 readLine() 读取空行,而逻辑上它不应该但是我该如何改变它?
readLine()reads everything up to \n. That's how it can check for a new line. You've got \nand nothing before it, so your line will consist of ""because \nis omitted.
readLine()读取所有内容\n。这就是它如何检查新行的方式。\n在它之前你什么都没有,所以你的行将包含""因为\n被省略。
Slight enhancement
轻微增强
If you want to make sure your line definitely has a name and a property in it, then you can use some simple regex.
如果你想确保你的行肯定有一个名字和一个属性,那么你可以使用一些简单的正则表达式。
if(s.CurrentLine.matches("\w+=\w+"))
// Evaluates to any letter, 1 or moe times, followd by an "=", followed by letters.
回答by Reporter
One way is the use of method trim()to check is the current line empty or not:
一种方法是使用方法trim()来检查当前行是否为空:
sCurrentLine = br.readLine();
while ((sCurrentLine) != null)
{
If ("".equals(sCurrentLine.trim()) == false)
{
config+=sCurrentLine.replace('=', ':');
sCurrentLine = br.readLine()
if(sCurrentLine!=null)
config+=",";
}
}
config+="}";
回答by csk
The following code will check regex for empty line . this should also do
以下代码将检查正则表达式是否为空行。这也应该做
if (sCurrentLine != null){
if (!sCurrentLine.matches("^\s*$")) // matches empty lines
config += ",";
}

