带引号的Json数据验证,数字数据类型没有引号

时间:2017-10-04 19:49:08

标签: java json streaming sqoop

使用不同方法(Sqoop /实时流)从Oracle DB提取的数据和以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)    

我尝试使用bellow 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  

0 个答案:

没有答案