将Java属性文件转换为JSON字符串 [英] Converting Java properties file into JSON string

查看:396
本文介绍了将Java属性文件转换为JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的解析器,将包含name=value对中的条目的java属性文件转换为json字符串. 下面是代码.该代码要求每个条目都在新行中:

I'm writing a simple parser to convert a java properties file which contains entries in name=value pair to a json string. Below is the code. The code requires that each entry is in a new line:

      sCurrentLine = br.readLine();
        while ((sCurrentLine) != null)
        {                           
              config+=sCurrentLine.replace('=', ':');
              sCurrentLine = br.readLine()
              if(sCurrentLine!=null)
              config+=",";
        }
        config+="}";

该函数可以正常工作,除非属性文件中有多余的新行. (例如:假设我将最后一个条目写在props文件中,然后按两次Enter,该文件将在最后一个条目之后包含两个空的新行).虽然预期输出为{name1:value1,name2:value2},但在上述情况下,如果存在多余的新行,则输出为{name1:value1,name2:value2,}.尾随,的数量随空白行的增加而增加.

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.

我知道它是因为readLine()在逻辑上不应该读取空行,但是我该如何更改呢?

I know its because the readLine() reads the empty line while logically it shouldn't but how do I change this?

推荐答案

可以使用contains()方法解决.只需确保您的行中存在"=".

This could be solved using the contains() method. Simply ensure the existence of a "=" in your line..

while ((sCurrentLine) != null)
    {
          if(sCurrentLine.contains("=") {                           
              config+=sCurrentLine.replace('=', ':');
              sCurrentLine = br.readLine()
              if(sCurrentLine!=null)
                  config+=",";
          }
    }

示例

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.

我知道它是因为readLine()在逻辑上不应该读取空行,但是我该如何更改呢?

I know its because the readLine() reads the empty line while logically it shouldn't but how do I change this?

readLine()读取所有内容,直到\n.这样便可以检查新行.您已经有了\n且之前没有任何内容,因此您的行将由""组成,因为省略了\n.

readLine() reads everything up to \n. That's how it can check for a new line. You've got \n and nothing before it, so your line will consist of "" because \n is omitted.

轻微增强

如果要确保行中确实有名称和属性,则可以使用一些简单的正则表达式.

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.

这篇关于将Java属性文件转换为JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆