Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How can i access this response from server using javascript. This is the json encoded data.

      [{"cid":"1234","city":"value1","district":"value2","state":"value3"}]

Thanks in advance.

This is the ajax code:

 function cityauto(ctid){
     var city = document.getElementById(ctid);
     if(city.value.length > 3){
         $.ajax({
             type: "GET",
             url: "city.php",
             data: {term: city.value},
             success: function (data){
                 alert(data);
             }
         });
 }

Html code:

   <input type="text" name="city" id="cty" onblur="cityauto(this.id);" />

onblur i am getting above response from php file in alert box now i need to access that values in javscript.

share|improve this question
    
give me your code – Karthick Kumar Jan 10 '14 at 9:43
    
Please provide more info about what you wan't to do, what you have tried and your code if any – Robert W. Hunter Jan 10 '14 at 9:44
2  
I know a guy called Jason, maybe he will know... JSON!!! – musefan Jan 10 '14 at 9:44
    
I dont know how to access it since i am new to json – NiksD Jan 10 '14 at 9:45
    
Please include information about how you are getting that response from the server, show some code – musefan Jan 10 '14 at 9:46
up vote 11 down vote accepted

Assuming the JSON is returned as a string:

var data = '[{"cid":"1234","city":"value1","district":"value2","state":"value3"}]';
// Parse the data as json
var obj = JSON.parse(data)
// Access the ojbect:
console.log(obj);
console.log(obj[0]);     // == Object {cid: "1234", city: "value1", district: "value2", state: "value3"} 
console.log(obj[0].cid); // == 1234

The [0] is to access the first object inside the JSON, which is an array. Then you just add .name, where 'name' is the name of the variable you want. (like .cid).

If the JSON is already a object, you can skip the JSON.parse():

var obj = [{"cid":"1234","city":"value1","district":"value2","state":"value3"}];

And access it like the example above.
(In that case, this question is more about accessing JavaScript objects, instead of JSON)

In your case, you can access the data like this:

success: function (data){
    var obj = JSON.parse(data);
    // Do stuff with `obj` here.
}
share|improve this answer
    
Thanks. It is working fine. – NiksD Jan 10 '14 at 10:21
    
@NiksD: No problem! Please consider marking this as the answer ;) – Cerbrus Jan 10 '14 at 10:27
    
marked already. :) – NiksD Jan 10 '14 at 10:52

If this is the only response data then you can access as:

var data = [{"cid":"1234","city":"value1","district":"value2","state":"value3"}];
console.log(data[0].cid);

# "1234"
share|improve this answer
1  
Thanks for reply. but JSON.parse(data) is required there. before console statement. – NiksD Jan 10 '14 at 10:22
    
Are you getting string data in success callback of ajax? – ram Jan 10 '14 at 10:32
    
m getting array of data as mentioned above. – NiksD Jan 10 '14 at 10:53
    
Then you don't need to do JSON.parse(data). JSON.parse is used in case your response is string containing json data as mentioned in 'Cerbrus' solution below. – ram Jan 10 '14 at 10:56
    
Yes it is ! Thanks for replying. – NiksD Jan 10 '14 at 11:13

If you recive that response from a page then you can try $.getJSON method using jQuery

$.getJSON("yourwebsite.com/yourpage", function( data ) {
      console.log(data);
});

If you want to use it as a variable, from plain JSON (not accesing to it from another page/request)

var data = '[{"cid":"1234","city":"value1","district":"value2","state":"value3"}]';

Then you can use

$.each( data, function( key, val ) {
          console.log(key + ":" + val);
  });

to access json data in a foreach routine...

Or data[key].innerykey for example data[0].cid 0 is your first array of data, and .cid is the key that you want to retrieve, it can be cid city or whatever you want, if you have more, you can use different number in data[X]

share|improve this answer
    
And where might one find the function $.getJSON? – musefan Jan 10 '14 at 9:48
    
forgot to tell about jquery :P – Robert W. Hunter Jan 10 '14 at 9:50
    
Thanks for reply @user1381537 – NiksD Jan 10 '14 at 10:24
    
I would like to know why the negative votes, my answer is valid, I explained how to use json in two different ways, thank you – Robert W. Hunter Jan 10 '14 at 11:16
    
@user1381537 Because nowhere in the question, nor in the code does the author of the question mention jQuery. jQuery is not the solution to everything and in most cases, if someone does not ask for jQuery, don't post it or you'll get downvoted. – Butt4cak3 Jan 15 '14 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.