Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用双引号括住所有JSON整数值&引用;使用正则表达式_Java_Regex_Json - Fatal编程技术网

Java 用双引号括住所有JSON整数值&引用;使用正则表达式

Java 用双引号括住所有JSON整数值&引用;使用正则表达式,java,regex,json,Java,Regex,Json,我一直在为这个解决方案绞尽脑汁,我有如下JSON字符串 {"Nodes": [ { "Node_id": 10023 }, { "Node_id": 10056 }, { "Node_id":00000 } ], "u

我一直在为这个解决方案绞尽脑汁,我有如下JSON字符串

{"Nodes":
    [
        {
            "Node_id": 10023
        },
        {
            "Node_id": 10056
        },
        {
            "Node_id":00000
        }
    ],
    "utc":136199375611
}
如何将其转换为以下格式

{"Nodes":
    [
        {
            "Node_id": "10023"
        },
        {
            "Node_id": "10056"
        },
        {
            "Node_id":"00000"
        }
    ],
    "utc":"136199375611"
}
现在,我想用双引号“integer value”对所有整数值进行编码,我如何在正则表达式中使用java模式和matcher类,甚至子字符串类进行编码。我们将非常感谢你的帮助


编辑 原始JSON格式如下所示

{
    "Nodes": [
        {
            "Node_id": "10023",
            "count": 1
        },
        {
            "Node_id": "10056",
            "count": 2
        },
        {
            "Node_id": "+00000",
            "count": 1
        },
        {
            "Node_id": "-00000",
            "count": "6"
        }
    ],
    "utc": "136199375611",
    "DeliveryTime": "Tue 23rd jun 2014 12:45 AM",
    "Ifr": "2333"
}                    
只是一个快速修复

jsonString.replaceAll("(\\d+)","\"$1\"")

但我建议您使用适当的JSON解析器或GSON库将其解析为Java对象,然后将整数值转换为字符串,最后将该Java对象转换回JSON字符串

  • 使用以下命令将JSON字符串转换为Java对象
  • 使用从Java转换回JSON字符串
示例代码:

class NodesDetail{
    private ArrayList<NoteId> Nodes;
    private String utc;
}

class NoteId{
    private String Node_id;
}

...
NodesDetail obj = new Gson().fromJson(reader, NodesDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));

编辑 只需根据JSON字符串编辑POJO类以进行匹配

class NodesDetail{
    private ArrayList<NoteId> Nodes;
    private String utc;
    private String DeliveryTime;
    private String Ifr;
}

class NoteId{
    private String Node_id;
    private String count;
}
...
NodesDetail obj = new Gson().fromJson(reader, NodesDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));

感谢您的快速回复,@Braj现在我有了这样的真实场景,原始JSON格式如下,{“Nodes”:[{“Node_id”:“10023”,“count”:1},{“Node_id”:“10056”,“count”:2},{“Node_id”:“+00000”,“count”:1}{“Node_id”:“-00000”,“计数”:“6”}],“utc”:“136199375611”,“交货时间”:“2014年6月23日星期二上午12:45”,“Ifr”:“2333”},如果我现在做str替换,有些是对的,有些是错的,对于这一个,你将如何用正则表达式来做它。谢谢在adv中,请在你的问题中把它作为一个编辑放在最后。只需在最后添加它,不要替换现有的文本。非常感谢你,你能帮我做正则表达式吗?当然为什么不能,但是你呢必须在上面单独发布问题,因为这与这个问题无关。
class NodesDetail{
    private ArrayList<NoteId> Nodes;
    private String utc;
    private String DeliveryTime;
    private String Ifr;
}

class NoteId{
    private String Node_id;
    private String count;
}
...
NodesDetail obj = new Gson().fromJson(reader, NodesDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
{
  "Nodes": [
    {
      "Node_id": "10023",
      "count": "1"
    },
    {
      "Node_id": "10056",
      "count": "2"
    },
    {
      "Node_id": "+00000",
      "count": "1"
    },
    {
      "Node_id": "-00000",
      "count": "6"
    }
  ],
  "utc": "136199375611",
  "DeliveryTime": "Tue 23rd jun 2014 12:45 AM",
  "Ifr": "2333"
}