0

This is the json encoded string using php :

{"customer_id":"1","customer_name":"dd"}

I want to fetch the value customer_name from above string:

My code:

var obj=json.parse(data);
  $.each(obj,function(item){
     item.customer_name                 
  });

Please help

0

5 Answers 5

3

You don't need the $.each. Just call obj.customer_name after you parse the JSON. You only need $.each if your data is an array.

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

Comments

1

object.key will give you the value.

obj = {"customer_id":"1","customer_name":"dd"};
obj.customer_name;

Comments

1

I think this will do the job

var obj=JSON.parse(data);
alert(obj.customer_name);

Comments

1

You can get all data using obj. like obj.customer_name try

var obj = JSON.parse(data);
alert(obj.customer_name);

1 Comment

You shouldn't create JSON in PHP using literals, you should use json_encode.
1

JSON suppose to be an array of objects

Make your JSON look like this:

var obj = [{customer_id:"1",customer_name:"dd"}];

Then iterate like this using jquery

var obj = [{customer_id:"1",customer_name:"dd"}];

$.each(obj,function(key,val){
     console.log(val.customer_name);                 
  });

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.