如何将Blob文本(从Oracle DB查询接收)转换为数组或JSON对象,以便我可以获取所需的项值?
例如:
var connection = new ActiveXObject("ADODB.Connection");
var connectionstring = "some conection string";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
var queryString="Select COLUMNNAME from SOMETABLE where id=123456"
rs.Open(queryString, connection);
var val=rs.fields(0).value;
这里的 val
我的单元格blob值如下所示
\[(\22transaction_id\22,variant \224937178\22);(\22deal_tracking_id\22,variant \224876812\22);(\22instrument_type\22,variant \22COMM-VT\22);(\22internal_portfolio\22,variant \22MA_STRUCTURED_BUY\22);(\22internal_contact\22,variant \22C19850\22);(\22transaction_status\22,variant \22Validated\22);(\22last_update_user\22,variant \22ENDUR_MGR02\22);(\22last_updated\22,variant 2015-11-25T02\3a32\3a00);(\22commodity\22,variant \22Natural Gas\22);(\22source_system\22,variant \22EndurGO\22);(\22input_date\22,variant 2015-10-06T00\3a00\3a00);(\22last_exercise_date\22,variant 2015-11-27T14\3a00\3a00);(\22product_type\22,variant 103)]
从上面的blob我想得到" transaction_id" 如何在Javascript中将上面的值转换为某些数组或JSON格式?
答案 0 :(得分:0)
要得到这个:
[{
"transaction_id" : "4937178",
"deal_tracking_id" : "4876812",
"instrument_type" : "COMM-VT",
"internal_portfolio" : "MA_STRUCTURED_BUY",
"internal_contact" : "C19850",
"transaction_status" : "Validated",
"last_update_user" : "ENDUR_MGR02",
"last_updated" : "2015-11-25T02:32:00",
"commodity" : "Natural Gas",
"source_system" : "EndurGO",
"input_date" : "2015-10-06T00:00:00",
"last_exercise_date" : "2015-11-27T14:00:00",
"product_type" : "103"
}]
你必须:
为了获得有效的JSON,所有值都应该是String类型(Numbers除外)。
答案 1 :(得分:0)
transaction_id
的值应该是变量号吗?和variant \224937178\22);
一样?
假设您一直在寻找该变体编号,并且您收到的回复的格式始终相同,您只需在Javascript中使用Regex即可提取ID。
var filter = /(\b[\w]{14}\b)\S,\b[\w]{7}\s\W(\d+)/.exec(val);
if (filter === null) {
console.log("There was an error, the Regex came up empty.");
} else {
if (filter[1] === "transaction_id") {
var transaction_id = filter[2];
} else {
var transaction_id = null;
}
}
您可以运行下面的代码段,看看如何在页面上输入HTML。
var val = "\[(\22transaction_id\22,variant \224937178\22);(\22deal_tracking_id\22,variant \224876812\22);(\22instrument_type\22,variant \22COMM-VT\22);(\22internal_portfolio\22,variant \22MA_STRUCTURED_BUY\22);(\22internal_contact\22,variant \22C19850\22);(\22transaction_status\22,variant \22Validated\22);(\22last_update_user\22,variant \22ENDUR_MGR02\22);(\22last_updated\22,variant 2015-11-25T02\3a32\3a00);(\22commodity\22,variant \22Natural Gas\22);(\22source_system\22,variant \22EndurGO\22);(\22input_date\22,variant 2015-10-06T00\3a00\3a00);(\22last_exercise_date\22,variant 2015-11-27T14\3a00\3a00);(\22product_type\22,variant 103)]";
var filter = /(\b[\w]{14}\b)\S,\b[\w]{7}\s\W(\d+)/.exec(val);
if (filter === null) {
$('#test').html("<p class='error'>Uh oh, the regex came up empty");
} else {
var transaction_id = filter[2];
if (filter[1] === "transaction_id") {
$('#test').html("<p class='success'>Transaction ID: " + transaction_id + "</p>");
} else {
$('#test').html("<p class='error'>Uh oh, there was no <b>transaction_id</b> present</p>");
}
}
&#13;
.error {
background-color: red;
color: #fff;
font-weight: bold;
}
.success {
background-color: lightgreen;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test">
</div>
&#13;