jQuery-从JSON Stringify获取价值 [英] jQuery - Get value from JSON Stringify

查看:114
本文介绍了jQuery-从JSON Stringify获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,我需要从以下表格获取值:

I have a form, that I need to get the values from:

var formConfig = JSON.stringify($("#bookingform").serializeArray());

其返回以下:

[{"name":"client_id","value":"1"},{"name":"consignee_id","value":""},{"name":"client","value":"DAKO"},{"name":"model","value":"2"},{"name":"type","value":"2"},{"name":"temperatur","value":"2"},{"name":"shipper_labels","value":"1"},{"name":"batteri_labels","value":"1"},{"name":"batteri_number","value":"2222"},{"name":"pickup","value":"April 27, 2017 18:25"},{"name":"intern_marks","value":"fdsfads"},{"name":"extern_marks","value":"sadsfdsf"},{"name":"consignee","value":""},{"name":"marks","value":""}]

然后,我需要从JSON字符串访问上述值,我正在使用此函数执行以下操作:

I then need to access above values from the JSON string, which I am using this function to:

var confirmBooking = function(element, setting, correct, notcorrect) {
    $('.confirm-booking')
    .find(element)
    .html(setting === 1 ? correct : notcorrect);
};

想法是,我可以使用上面的功能:

The thought is, I can use above function:

confirmBooking('.config_batteri', formConfig.client_id, "Yes", "No");

以此类推.

formConfig.client_id应该返回1,因为这是上面JSON字符串中的值.

formConfig.client_id should return 1, since that's the value in the JSON string above.

但是它返回undefined:

console.log(formConfig.client_id); // Returns "undefined"

推荐答案

您需要使用JSON.parse()使其再次成为常规JavaScript对象.

You need to use JSON.parse(), to make it a regular JavaScript object again.

顾名思义,JSON.stringify()的作用是将其设置为字符串.那只是一个常规字符串,代表JSON数据.它没有神奇的JSON数据属性.

What JSON.stringify() does is, as the name implies, make it a string. That is just a regular string, that represents JSON data. It doesn't magically have the JSON data properties.

此外,serializeArray()返回{name: ..., value: ...}对象的数组.如果未将其转换为JSON字符串,则通过执行formConfig.client_id仍不能轻易访问结果.您必须遍历formConfig数组,以找到所需的名称/值对.

Furthermore serializeArray() returns an array of {name: ..., value: ...} objects. If you didn't turn it into a JSON string, the result still could not easily be accessed by doing formConfig.client_id. You'd have to loop through the formConfig array, to find the desired name/value pair.

从它的外观看,如果您只是要在同一脚本中使用它,则根本不需要将JavaScript对象转换为JSON(除非您可能将JSON字符串发送到其他地方)

From the looks of it, you don't need to turn the JavaScript object into JSON at all, if you are just going to use it in the same script (unless you are going to send the JSON string somewhere else perhaps).

基于OP的注释,我假设OP只想访问特定表单元素的值.使用jQuery,可以轻松实现以下目标:

Based on OP's comments, I'm assuming OP just want to access a particular form element's value. Using jQuery, this can easily be accomplished with:

// get the value of the client_id element (assuming it's an input element)
var client_id = $( '#bookingform input[name="client_id"]' ).val();
// call the function
confirmBooking( '.config_batteri', client_id, "Yes", "No" );

如果您想要一个具有所有表单值的对象,请查看

If you want to have an object with all the form values, have a look at the answers in this question.

这篇关于jQuery-从JSON Stringify获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆