我有这个JSON数组,我想要的是单独获取密码字段
var user = [ { _id: 5902086ecbc0dd11e4870fd9,
password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
email: 'jv100@gmail.com',
lastName: 'v',
firstName: 'j',
updatedDate: 2017-04-27T15:04:14.483Z,
createdDate: 2017-04-27T15:04:14.483Z } ]
我尝试使用此代码解析它
var obj = JSON.parse(user);
console.log(user.password);
但仍未定义。
答案 0 :(得分:2)
用户已经是JSON,因此您无需解析它。但是,它是一个数组。因此,如果您认为它是一个数组,那么您需要使用以下代码来访问密码:
public void sendOrder(String orderSide, int parentOrderId) {
System.out.println("sendOrder is called");
Order order = new Order();
parentOrderId = nextOrderID + 1;
order.orderId(nextOrderID + 1);
//order.m_action = "BUY";
order.action(orderSide);
//order.m_totalQuantity = 1;
order.totalQuantity(20000);
//order.m_lmtPrice = 0.01;
System.out.println("sendOrder, this.high = " + this.high);
DecimalFormat df = new DecimalFormat("0.00000"); // TODO get digit according
String formatedEntryPrice = df.format(this.high);
System.out.println("formatedEntryPrice = " + formatedEntryPrice);
order.lmtPrice(Double.valueOf("1.00")); // TODO stop limit at prior day high
//order.auxPrice(this.high);
//order.auxPrice(Double.valueOf(formatedEntryPrice));
//order.m_orderType = "LMT";
order.orderType("LMT");
//order.m_account = "U1836253";
order.account("U1836253");
order.transmit(false);
System.out.println("after creating order");
if (orderSide == "BUY"){
Order stopLoss = new Order();
order.action ("SELL");
stopLoss.orderId(nextOrderID + 1);
order.orderType ("TRAIL");
order.totalQuantity (20000);
order.trailingPercent (1);
order.trailStopPrice(low);
stopLoss.parentId(nextOrderID) ;
order.transmit(true);
}
if (orderSide == "SELL"){
Order stopLoss = new Order();
order.action ("BUY");
stopLoss.orderId(nextOrderID + 1);
order.orderType ("TRAIL");
order.totalQuantity (20000);
order.trailingPercent (1);
order.trailStopPrice(low);
stopLoss.parentId(nextOrderID) ;
order.transmit(true);
}
m_s.placeOrder(nextOrderID + 1, contract, order);
System.out.println("sendOrder is finished");
nextOrderID = nextOrderID +1;
client.reqIds(Integer.valueOf("1"));
System.out.println("sendOrder is finished 2");
}
答案 1 :(得分:2)
它已经是一个无需解析的数组。您可以通过以下方式访问您的酒店:
console.log(user[0].password);
您无法使用user.password
访问您的媒体资源,因为user
变量不是对象,它是一个数组,您的对象存储在数组的零索引处。
答案 2 :(得分:2)
您已经拥有JSON object
。因此,无需再次解析它。
<强>样本强>
var user = [{ _id: "5902086ecbc0dd11e4870fd9",
password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
email: 'jv100@gmail.com',
lastName: 'v',
firstName: 'j',
updatedDate: "2017-04-27T15:04:14.483Z",
createdDate: "2017-04-27T15:04:14.483Z" } ];
var password = user[0].password;
console.log(password);
&#13;
答案 3 :(得分:1)
变量&#39;用户&#39;不是JSON数组。它是一个以单个Javascript对象为元素的数组。 JSON.parse(arg)只能用于将JSON字符串解析为普通的Javascript对象。话虽这么说,要访问数组中的javascript对象,你可以这样做:
var userData = user[0];
要访问变量userData中的密码,您可以执行以下操作:
var password = userData.password;
使用以下命令将密码记录到控制台:
console.log(password);
答案 4 :(得分:1)
试试这个:
var user = [ {_id:'5902086ecbc0dd11e4870fd9',password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',email: 'jv100@gmail.com',lastName: 'v',firstName: 'j',updatedDate: '2017-04-27T15:04:14.483Z',createdDate:' 2017-04-27T15:04:14.483Z' } ];
var obj = user[0];
console.log(obj.password);