0

I am using JSON.Stringify() in my javascript...

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 :

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

Please help

2
  • Why do you write jsonProp = jsonProp + JSON.stringify(json); at all instead of jsonProp = JSON.stringify(json);? Commented Jul 5, 2017 at 7:55
  • @t.niese because it the content have a grid table and the val is the value of the index of that grid table Commented Jul 5, 2017 at 10:17

4 Answers 4

3

Because you declared jsonProp but didn't define it: var jsonProp;. When you don't set a value to a variable, it will be undefined.

Sign up to request clarification or add additional context in comments.

4 Comments

i declared it as var jsonProp, but still undefined
@Richard It's still
Like I said, you just declared it but didn't define it. Set a value to it.
whoa, i undrestand now. Thankyou!
1

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. That is how it works. So, when you concatenate it:

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

This happens.

Comments

0

Your variable jsonBiz wasn't defined yet.

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.

2 Comments

sorry, i my bad, jsonProp/jsonBiz is var, in my script "var jsonProp;", i edited my question. thankyou
@Richard: yes, but that doesn't change the problem, just imagine my answer also says jsonProp wherever I wrote jsonBiz.
0

you need to define jsonBiz before using it.

var jsonBiz = "";

jsonBiz = jsonBiz + JSON.stringify(json);

please try this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.