简体   繁体   English

如何从JSON对象获取简单数据

[英]How can I get simple data from JSON object

How can I get the value from this object? 如何从此对象获取值?

var descipt = "{'type':'" + $('#medi-type option:selected').val() +"',"+
        "'weight':" + $('#weight').val() +","+
        "'weight-type':'" + $('#weight-type option:selected').val() +"',"+
        "'dose':'" + $('#medicine-dose').val() +"',"+
        "'dose-type':'" + $('#dose-type option:selected').val() +"',"+
        "'day-time':'" + morning +"',"+
        "'noon-time':'" + noon +"',"+
        "'night-time':'" + night +"',"+
        "'after':'" + after +"',"+
        "'before':'" + before +"'}";

alert(descipt.weight);

how to get weight from the object. 如何从物体上获得重量。

That's not a JSON object, that's a string. 那不是JSON对象,那是一个字符串。 You should first parse it to a JSON object with 您应该先将其解析为JSON对象

var desciptObject = JSON.parse(descipt);

and then you can read weight with 然后你可以阅读体重

weight = desciptObject.weight;

first you need to convert that to a JavaScript object with: 首先,您需要将其转换为JavaScript对象:

var obj = JSON.parse(descipt);

and after that use like this: 之后使用这样:

alert(obj.weight);

in fact this is an string and you can not access like a object to nodes. 实际上这是一个字符串,你不能像对象那样访问节点。

Use JSON.parse 使用JSON.parse

var jsonObj = JSON.parse(descipt);
var weight = jsonObj.weight;
var weighttype = jsonObj["weight-type"]; // jsonObj.weight-type  will throw error

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

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