我正在使用节点js中的web服务,它以这种奇怪的格式返回响应。我必须通过传递键名来提取某些值,并且我想将响应转换为JSON以便于解析。我应该怎么做呢?
这是我从网络服务
获得的Data Source=*******;Initial Catalog=****;User ID=*******;Password=
*******;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Conn
ect Timeout=180;Application Name=*****
我想提取DataSource,UserID,密码。
答案 0 :(得分:2)
querystring
模块可以做这种事情并且#34;免费&#34;。< / p>
代码
const qs = require('querystring');
const s = `Data Source=*******;Initial Catalog=****;User ID=*******;Password=*******;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=*****`;
console.log(qs.parse(s, ';', '='));
输出:
{ 'Data Source': '*******',
'Initial Catalog': '****',
'User ID': '*******',
Password: '*******',
MultipleActiveResultSets: 'True',
'Min Pool Size': '5',
'Max Pool Size': '5000',
'Connect Timeout': '180',
'Application Name': '*****' }
答案 1 :(得分:1)
您可以使用String split轻松解析它:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<br/>
<button type="button" class="btn btn-primary" style="margin-right:30px;">Nazaj</button>
<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span></button>
<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-down"></span></button>
<input type="text" class="form-control" style="width:50px;display:inline-block;position:relative;top:1px;"/><h2 style="display:inline-block;position:relative;top:5px;padding-left:3px;">/</h2>
<input type="text" class="form-control" style="width:50px;display:inline-block;position:relative;top:1px;"/>
<button type="button" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-up"></span></button>
<button type="button" class="btn btn-primary" style="margin-right:30px;"><span class="glyphicon glyphicon-arrow-down" ></span></button>
<button type="button" class="btn btn-success">Shrani</button>
<br/>
<br/>
<div class="btn-toolbar">
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
</div>
<div class="btn-toolbar">
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
</div>
<div class="btn-toolbar">
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
<button type="button" class="btn btn-primary col-sm-1">1/2</button>
</div>
您可以对其进行操作,删除或提取值,最后使用const obj = response.split(';', 2).reduce((json, pair) => {
const tokens = pair.split('=', 2);
json[tokens[0]] = tokens[1];
return json;
}, {});
在JSON中对其进行编码。
注意:正如Oleg V. Volkov评论的那样,如果源字符串包含键或值中的分隔符(';'和'='),则它将不起作用。
答案 2 :(得分:0)
如果返回的模式是常量,则可以使用regexp实现:
var result = str.match(/Source=(.*);Initial.*ID=(.*);Password=(.*);Multiple/)
然后从匹配的组中获取所需的值。
DataSource = result[1];
UserId = result[2];
Password = result[3];