java - 如何在不循环键的情况下检查嵌套 json 对象的键名称是否包含数字/特殊字符?

标签 java json regex talend

我正在尝试检查 JSON 对象的名称中是否包含数字/特殊字符,并且 JSON 对象是一个对象数组,每个对象本身都是一个嵌套的 JSON 对象,并且某些键也有一个对象数组。 我的情况是,如果我的 JSON 对象的键名称中包含数字,那么这是一条错误记录,所以我必须删除或跳过这些类型的 JSON 对象,我该怎么办。 在 Talend 中,如果我有如下所示的 JSON 对象,它将引发错误,请提供格式良好的 JSON

[{"abc":"abcd"},{"def":"abcd"},{"0":"saran"}]

因为它有 0 作为关键 talend 会抛出该错误。

以下是我的实际 JSON 对象

[
{"_id":"5dd71ec4ad611b6464f912eb","dimensions":{"0":"0[object Object]","container":1,"weigth":2,"height":253,"lenght":600,"width":400},"errorLocation":{"location":{"type":"Point","geometry":[]},"addressLines":[],"geocodeScore":0,"cleanScore":0},"execution":{"timer":{"timestamps":[]}}},
{"_id":"5ddb15c42fef196f91d279b1","dimensions":{"container":1,"weigth":2,"height":253,"lenght":600,"width":400},"errorLocation":{"location":{"type":"Point","geometry":[]},"addressLines":[],"geocodeScore":0,"cleanScore":0},"execution":{"timer":{"timestamps":[]}}}
]

所以我尝试过JAVA例程 这是我的 Java 例程代码

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.*;
public class Carrefour_Data_Remove_Issue {

    /**
     * helloExample: not return value, only print "hello" + message.
     *
     *
     * {talendTypes} String
     *
     * {Category} User Defined
     *
     * {param} string("world") input: The string need to be printed.
     *
     * {example} helloExemple("world") # hello world !.
     * @throws JSONException
     */
    public static String jsonInput(String message) throws JSONException {
    //String escapedString = StringEscapeUtils.unescapeJava(message);
    //message = message.trim();
    JSONArray jsonArray = new JSONArray(message);
    JSONArray jsonArrayOutput = new JSONArray();
    for (int i = 0, size = jsonArray.length(); i < size; i++)
    {
         JSONObject objectInArray = jsonArray.getJSONObject(i);
         //message = objectInArray.toString();
         //System.out.println(isJSONValid(message));
         JSONObject innerObject = objectInArray.getJSONObject("dimensions");
         if(innerObject.has("0")==false)
         {
         jsonArrayOutput.put(objectInArray);  
         }


         //System.out.println(innerObject);
    }
   //System.out.println(jsonArrayOutput);
    message = jsonArrayOutput.toString();
        return message;
    }
    public static boolean isJSONValid(String test) {
        try {
            new JSONObject(test);
        } catch (JSONException ex) {
        return false;

            // edited, to include @Arthur's comment
            // e.g. in case JSONArray is valid as well...
        }
        return true;
    }
} 

最佳答案

你可以尝试类似的方法

String jsonString = innerObject.toString();
boolean b = jsonString.matches(".*\"(.*\\W+.*)\":.*");
// if (b) smth
// else smth else

此正则表达式匹配带有特殊字符或数字的字符串,这些字符或数字前面是“,后面是”:

关于java - 如何在不循环键的情况下检查嵌套 json 对象的键名称是否包含数字/特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59267091/

相关文章:

java - 在 Spring 中解析 HTML 页面的问题 - Java

jquery - 使用 jquery 和 Json 时日期值显示格式错误

javascript - 按句点拆分字符串,但如果句点后有空格则不拆分

java - Hibernate 数据映射到子对象

java - 如何使用hibernate,HSQL按日期查询过滤器

在 django Rest api 中使用 POST 进行 Json 解析错误

ios - 使用 swiftyJSON 和 sqlite.swift 将基于 json 的数据添加到 sqlite 数据库中

用于在 Python 中重复字符串中字符的正则表达式

regex - Scala 正则表达式联合

java - 按位置将 JSONArray 映射到对象