简体   繁体   English

Javascript JSON.Stringify()未定义

[英]Javascript JSON.Stringify() what is undefined

I am using JSON.Stringify() in my javascript... 我在我的JavaScript中使用JSON.Stringify()...

var jsonProp;
var json = { };
json.prop= { };
json.prop.Brand = $('.Brand' + val).val();
json.prop.Name = $('.Name' + val).val();
json.prop.Desc = $('.Desc' + val).val();
json.prop.Address = $('.Address' + val).val();
json.prop.Phone = $('.Phone' + val).val();
json.prop.Tag = $('.Tag' + val).val();
json.prop.City = $('.City' + val).val();
json.prop.Status = $('.Status' + val).val();
jsonProp = jsonProp + JSON.stringify(json);

and the result is : 结果是:

"undefined{"prop":{"Brand":"","Name":"apotik AA","Desc":"","Address":"Address","Phone":"","Tag":"","City":"BEKASI","Status":"0"}}
{"prop":{"Brand":"","Name":"apotik AAaaaa. Bina Farma","Desc":"","Address":"Jl. RA. Kartini, Margahayu-Bekasi Tim., Kota Bks, Jawa Barat 17113","Phone":"","Tag":"","City":"BEKASI","Status":"0"}}"

why is "undefined" there? 为什么在那里“未定义”? how can I set it? 我该如何设置? all I want to have is a JSONArray like this : 我只想拥有一个像这样的JSONArray:

Prop{{"Brand":"value","Name":"value"},{"Brand":"value","Name":"value"}}

Please help 请帮忙

Because you declared jsonProp but didn't define it: var jsonProp; 因为您声明了jsonProp但未定义它: var jsonProp; . When you don't set a value to a variable, it will be undefined . 如果您没有为变量设置值,则它将是undefined

When you declare a variable without any unassigned value: 声明没有任何未分配值的变量时:

var jsonProp; // default value set to undefined by javascript.

Without any assignment. 没有任何分配。 What javascript does is, it assigns a default value to undefined . javascript的作用是,它将默认值分配给undefined That is how it works. 这就是它的工作方式。 So, when you concatenate it: 因此,当您串联它时:

jsonProp = jsonProp + JSON.stringify(json);
//         undefined + "{prop:{}}"

This happens. 有时候是这样的。

Your variable jsonBiz wasn't defined yet. 您的变量jsonBiz尚未定义。

So when you do 所以当你这样做

jsonBiz = jsonBiz + JSON.stringify(json)

You add an undefined value and a string. 您添加一个未定义的值和一个字符串。 The first value is turned into the string "undefined", and the two are added. 第一个值变成字符串“ undefined”,然后将两者相加。

you need to define jsonBiz before using it. 您需要先定义jsonBiz,然后再使用它。

var jsonBiz = "";

jsonBiz = jsonBiz + JSON.stringify(json);

please try this. 请尝试这个。

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

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