简体   繁体   English

在Javascript中将Blob文本转换为数组/ JSON对象

[英]Converting Blob text to array/JSON object in Javascript

How to convert Blob text(that is received from Oracle DB query) to array or JSON object , so that i can get the required items values? 如何将Blob文本(从Oracle DB查询接收到的文本)转换为数组或JSON对象,以便获得所需的项目值?

For ex: 例如:

            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;

Here in val i got cell blob value as like below 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)]

From the above blob i wanted to get the value of "transaction_id" How to convert above value to some Array or JSON format in Javascript?? 从上面的Blob中,我想获取“ transaction_id”的值。如何将上述值转换​​为Javascript中的某些Array或JSON格式?

To get this: 为了得到这个:

[{
  "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"
}]

You have to: 你必须:

  • Remove ( val.replace('(',''); 删除 val.replace('(','');
  • Remove \\22 val.replace('\\22',''); 删除\\ 22 val.replace('\\ 22','');
  • Replace ); 替换); with , val.replace(');',','); val.replace(');',',');
  • Replace ,variant with : val.replace(',variant',': '); 替换,变体:val.replace( '变体', ':');
  • Replace \\3a with : val.replace('\\3a',':'); \\ 3a替换为 val.replace('\\ 3a',':'); (Date format!) (日期格式!)
  • Replace [ with [{ val.replace('[','[{'); [替换为[{ val.replace('[','[{');
  • Replace ] with }] val.replace(']','}]'); ]}] val.replace( ']', '}]')取代;

also to get valid JSON all values should be String type (except Numbers). 为了获得有效的JSON,所有值都应为String类型(数字除外)。

Is the value of transaction_id supposed to be the variant number there? transaction_id的值是否应该是那里的变量号? As in variant \\224937178\\22); variant \\224937178\\22); ?

Assuming you're always looking for that variant number and the response you receive back is always formatted the same, you could simply use Regex in your Javascript to extract the ID. 假设您一直在寻找该变体编号,并且收到的响应始终采用相同的格式,那么您只需在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;
    }
}

You can run the code snippet below to see how it would work inputting HTML onto a page. 您可以运行下面的代码片段,查看将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>"); } } 
 .error { background-color: red; color: #fff; font-weight: bold; } .success { background-color: lightgreen; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="test"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM