json数据验证,用于数字数据类型的带引号和不带引号

b0zn9rqh  于 2021-06-03  发布在  Sqoop
关注(0)|答案(0)|浏览(284)

使用不同的方法(sqoop/real-timestreaming)从oracle数据库提取数据,并以json格式存储提取的数据。
java中的json数据验证:有时将来自oracle的数据类型数据转换为 column_id: "100" 有时 100 用引号括起来。如何使用java区分带引号和不带引号的数据。
带引号的json示例:

{
    "ID": "10",
    "location": "56",
    "DESCRIPTION": "Z LINE",
    "ORDER": "0"
}

不带引号的json示例:

{
    "D": 10,
    "location": 56,
    "DESCRIPTION": "Z LINE",
    "ORDER": 0
}

oracle表说明:

Name         Null?    Type         
------------ -------- ------------ 
ID NOT NULL NUMBER(10)   
LOCATION NOT NULL NUMBER(2)
DESCRIPTION  NOT NULL VARCHAR2(50) 
ORDER   NOT NULL NUMBER(3)

我试过使用下面的java程序,但都给出了相同的数据,没有引号:

import org.json.JSONObject;
JSONObject jsonObject=new JSONObject(inputJsonData);
Iterator<String> keys = jsonObject.keys();

while (keys.hasNext()) {
    String key=keys.next();
    System.out.println(" Key :" +key+"  Value :"+jsonObject.get(key));
}

我为自己的问题找到了解决办法。下面是解决这个问题的方案:

import org.json.JSONObject;
        import java.util.Iterator;
        public class JsonParserDataCheck {

public static void main(String[] args) {

    String inputJsonData_quote="{\"magic\": \"atMSG\", \"type\": \"DT\", \"headers\": null, \"messageSchemaId\": null, \"messageSchema\": null, \"message\": {\"data\":{\"ZONE_TYPE_ID\": \"10\", \"DESCRIPTION\": \"Z LINE\", \"PICK_ORDER\": 0,\"pharmacy_location\": \"56\"} , \"beforeData\": null, \"headers\": {\"operation\": \"REFRESH\", \"changeSequence\": \"\", \"timestamp\": \"\", \"streamPosition\": \"\", \"transactionId\": \"\"}}}";
    String inputJsonData_no_quotes="{\"magic\": \"atMSG\", \"type\": \"DT\", \"headers\": null, \"messageSchemaId\": null, \"messageSchema\": null, \"message\": {\"data\":{\"ZONE_TYPE_ID\": 10, \"DESCRIPTION\": \"Z LINE\", \"PICK_ORDER\": 0,\"pharmacy_location\": \"56\"} , \"beforeData\": null, \"headers\": {\"operation\": \"REFRESH\", \"changeSequence\": \"\", \"timestamp\": \"\", \"streamPosition\": \"\", \"transactionId\": \"\"}}}";
    JSONObject jsonObject=new JSONObject(inputJsonData_quote);
    JSONObject data= (JSONObject) ((JSONObject) jsonObject.get("message")).get("data");

    Object aObj = data.get("ZONE_TYPE_ID");
    if(aObj instanceof Integer){
        System.out.println("ZONE_TYPE_ID is Interger data type  " );
    } else  if(aObj instanceof String){
        System.out.println("ZONE_TYPE_ID is String data type  " );
    }

    jsonObject=new JSONObject(inputJsonData_no_quotes);
    data= (JSONObject) ((JSONObject) jsonObject.get("message")).get("data");

    aObj = data.get("ZONE_TYPE_ID");
    if(aObj instanceof Integer){
        System.out.println("ZONE_TYPE_ID is Interger data type  " );
    } else  if(aObj instanceof String){
        System.out.println("ZONE_TYPE_ID is String data type  " );
    }
}

}

Output :
    ZONE_TYPE_ID is String data type  
    ZONE_TYPE_ID is Interger data type

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题