0

I get the json on console as

 [Object]
 0: Object
 address: "soham"
 region: "soham"
 relevanceScore: "4"
 startDate: "2015-05-10"
 subscriptionType: "1"
 verificationStatus: "1"
 __proto__: Object
 length: 1
 __proto__: Array[0]

and my ajax call

 $(document).ready(function() 
 {
 ($.ajax({ 
 url:"allServices/getBusinessDetails.php?busid=<? echo $business_id; ?>",
 dataType: "json",
 success: function (jsondata) {
 var allval=jsondata; 
 console.log(allval);
}
 })); 
});

but when i try to take the value out of it as allval.address it gives me undefined. How to parse the object

asked May 2, 2015 at 6:51
3
  • 4
    And allval[0].address ? Commented May 2, 2015 at 6:53
  • Notice 0. it's an arrayed json "[{},{},{}]" - hence look at dystroy's access to the array ( once it's parsed) Commented May 2, 2015 at 6:53
  • thank you.. allval[0].address worked. Commented May 2, 2015 at 7:02

5 Answers 5

2

if you send data as json on server you can use responce.data but you can dumps json object and send it as string, and use var r = JSON.parse(responce)

answered May 2, 2015 at 7:01
Sign up to request clarification or add additional context in comments.

Comments

1

I will try to explain the actual situation :

Assume you have array of objects :

var g = [{a:1,b:2},{c:1,d:2}];

Let's convert it to json string :

var json = JSON.stringify(g); //"[{"a":1,"b":2},{"c":1,"d":2}]"

Now , this is your situation :

the server returns :

"[{"a":1,"b":2},{"c":1,"d":2}]"

How can you parse it ?

var parsed = JSON.parse(json);

Great. now let's look how the browser sees it :

enter image description here

so how would you access the first item ?

Right , via [0] like dystroy write in his comment.

answered May 2, 2015 at 7:04

Comments

0

you can try like this

$(document).ready(function() 
 {
 ($.ajax({ 
 url:"allServices/getBusinessDetails.php?busid=<? echo $business_id; ?>",
 dataType: "json",
 success: function (jsondata) {
 var allval=JSON.stringify(jsondata); 
 console.log(allval[0].address);
 }
 })); 
});

I'm not sure but i think its helpful to you.

answered May 2, 2015 at 6:53

2 Comments

Thank you, that's it.. it worked.. but what this allval[0] is for, it returns json and we can parse the value or get it directly , and why allval gives undifined
I'm not sure but when you want to parse your response then you try JSON.stringify(response) and i think your response is in array that contain only one object so i am use allval[0]. you want to address from this object so i am use allval[0].address.
0

Just use jQuery.parseJSON

like this var obj = jQuery.parseJSON( '{ "name": "John" }' );

see https://api.jquery.com/jquery.parsejson/

If you get undefined the data you are trying to parse is probebly unvalid. You can try if its valid here http://pro.jsonlint.com

answered May 2, 2015 at 7:00

Comments

0
$(document).ready(function(){
$.getJSON("allServices/getBusinessDetails.php?busid=<? echo $business_id; ?>",function(data){
 $.each(data,function(key,value){
 alert(data[key].attrib1name);
 alert(data[key].attrib2name); 
 console.log(value);
 });
 }); 
});

Or this approach

 $(document).ready(function(){
 $.getJSON("allServices/getBusinessDetails.php?busid=<? echo $business_id; ?>",function(data){
 parseJsonArray(data);
 });
function parseJsonArray(obj) {
 // Do something.
 $.each(obj, function (idx, obj1) {
 if (typeof obj1 == 'object') {
 parseJsonArray(obj1);
 } else {
 alert(idx+":"+obj1);
 }
 });
}
answered May 2, 2015 at 6:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.