私は大括弧で囲まれたオブジェクトを生成するapi呼び出しを行っています。角括弧内のオブジェクトから文字列を解析するにはどうすればいいですか
コード:
request.get(options, function(error, response, body) {
var JSONdata = (JSON.stringify(body));
var trip = JSON.parse(JSONdata).results;
console.log(trip);
});
ログイン:
[ { id: 'T_ec926d18c46e39d9',
url: 'https://api.automatic.com/trip/T_ec926d18c46e39d9/',
driver: 'https://api.automatic.com/user/U_0c1ee2ddcfea14be/',
vehicle: 'https://api.automatic.com/vehicle/C_7e050c914a0d2b97/',
duration_s: 447.3,
distance_m: 3375.3,
started_at: '2017-10-31T02:11:58.607000Z',
ended_at: '2017-10-31T02:19:25.918000Z',
start_timezone: 'America/Toronto',
end_timezone: 'America/Toronto',
tags: [],
idling_time_s: 0,
user: 'https://api.automatic.com/user/U_0c1ee2ddcfea14be/' } ]
私は、このオブジェクトのうち、 "distance_m" を解析しようとしているが、内の文字列結果に変換しています:
[{"id":"T_ec926d18c46e39d9","url":"https://api.automatic.com /trip/T_ec926d18c46e39d9/","driver":"https://api.automatic.com/user/U_0c1ee2ddcfea14be/","vehicle":"https://api.automatic.com/vehicle/C_7e050c914a0d2b97/","duration_s":447.3,"distance_m":3375.3,"started_at":"2017-10-31T02:11:58.607000Z","ended_at":"2017-10-31T02:19:25.918000Z","start_timezone":"America/Toronto","end_timezone":"America/Toronto","tags":[],"idling_time_s":0,"user":"https://api.automatic.com/user/U_0c1ee2ddcfea14be/"}]
私は何も解析できません(角括弧のために仮定しています)
のいずれか(ここで示したとおりHow to remove square brackets in string using regex?)私は正規表現で角括弧を交換しようとしたが、それは動作しません、私は変数に「distance_m」値を抽出するにはどうすればよい
?
トリップ[0] .distance_m –