有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将JSON消息转换为具有空字段的avro模式的有效JSON?

我想向带有avro模式的卡夫卡主题发送一条JSON消息

avro模式允许多种类型:

{  
   "name":"typeA",
   "type":[  
      "int",
      "null"
   ],
   "default":null
}

如果该值为null,则一切正常。如果类型是int,如本例所示,则必须显式指定。看到这张票了吗

我有一个JSON:

{
   "typeA":12345,
   "typeB":[
      {
         "feature":1,
         "value":"1"
      },
      {
         "feature":2,
         "value":"2"
      }
   ],
   "typeC":[
      {
         "a":12345,
         "data":[
            {
               "a":12345,
               "b":[
                  12345,
                  12345,
                  12345
               ]
            }
         ]
      }
   ]
}

我想转换成这个JSON:

{
   "typeA":{
      "int":12345
   },
   "typeB":{
      "array":[
         {
            "feature":1,
            "value":"1"
         },
         {
            "feature":2,
            "value":"2"
         }
      ]
   },
   "typeC":{
      "array":[
         {
            "a":12345,
            "data":[
               {
                  "a":12345,
                  "b":[
                     12345,
                     12345,
                     12345
                  ]
               }
            ]
         }
      ]
   }
}

是否可以将"typeA":12345转换为"typeA":{"int":12345}?有没有一个简单的方法来处理这个问题

我知道每个字段的类型,所以我可以在JAVA中使用正则表达式:

json.replaceAll("typeA\":([^,]*),\"", "typeA\":{\"int\":$1},\"");

很难处理数组或最后一个JSON元素。我怎样才能解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    我可以将typeA转换为:

    "typeA":{
        "int":12345
    },
    

    但是typeBtypeC对我来说太难了,因为我无法精确匹配。当我试图用数组替换typeB时。另一个地方也被替换了,这是我们不想要的

    如果你或其他人可以解决这个问题,那么typeC也可以很容易地解决。因为typeBtypeC是相似的。我也很好奇解决方案是什么。所以让我知道

    现在我将分享我如何修复typeA。以下是Java代码:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
    
        public static void main(String[] args) {
            String line = "{\r\n" +
                    "   \"typeA\":12345,\r\n" +
                    "   \"typeB\":[\r\n" +
                    "      {\r\n" +
                    "         \"feature\":1,\r\n" +
                    "         \"value\":\"1\"\r\n" +
                    "      },\r\n" +
                    "      {\r\n" +
                    "         \"feature\":2,\r\n" +
                    "         \"value\":\"2\"\r\n" +
                    "      }\r\n" +
                    "   ],\r\n" +
                    "   \"typeC\":[\r\n" +
                    "      {\r\n" +
                    "         \"a\":12345,\r\n" +
                    "         \"data\":[\r\n" +
                    "            {\r\n" +
                    "               \"a\":12345,\r\n" +
                    "               \"b\":[\r\n" +
                    "                  12345,\r\n" +
                    "                  12345,\r\n" +
                    "                  12345\r\n" +
                    "               ]\r\n" +
                    "            }\r\n" +
                    "         ]\r\n" +
                    "      }\r\n" +
                    "   ]\r\n" +
                    "}";
    
            String regex = "(\\\"type[A-Z]\\\"):(\\d*)|(\\[.*)|(.*\\])";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(line);
            while (matcher.find()) {
                 if(matcher.group().equals("\"typeA\":12345")) {
                     String regex2 = "(\\\"typeA\\\"):(\\d*)";
                     line = line.replaceAll(regex2, "$1:{\"int\":$2}");
                 }
    
                 if(matcher.group().equals("\"typeB\":") ) {
                     //I couldn't finish this typeB, because it's too difficult
    //                 String regex3 = "(\\\"type[B]\\\"):|(\\s{3}],)";
    //                 line = line.replaceAll(regex3, "$1 :{ array: $2 ");
                 }
            }
             System.out.println("line: " + line);
        }
    }
    

    首先,我使用了这个正则表达式。正则表达式为我们提供了几个我们想要查看的组

    最终,while循环遇到"typeA":12345 这就是我们使用正则表达式的地方。我们使用该正则表达式将typeA转换为:

    "typeA":{
        "int":12345
    },