-1

I search for this everywhere but didn't get answer for this.I am having one json object after encoding php array like:

{
"6":{"brand_id":"6","brand_logo":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/account_images\/457974\/max_h-1000+max_w-1000\/tn.jpg","brand_discovered_count":"2","brand_name":"Not Human Clothing","brand_template_id":"3","products":[{"product_id":"9","product_price":"19.99","product_images":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/product_images\/131442291\/-\/blackpom2.jpg"}]},

"7":{"brand_id":"7","brand_logo":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/account_images\/465045\/max_h-1000+max_w-1000\/frontttt.jpeg","brand_discovered_count":"3","brand_name":"Trill LOVE","brand_template_id":"2","products":[{"product_id":"11","product_price":"49.99","product_images":"http:\/\/images.cdn.bigcartel.com\/bigcartel\/product_images\/134025228\/-\/l3.jpeg"}]}

}

How can i read this in javascript in ajax

$.ajax({
    type:'POST',
    dataType: 'json',
    data: 'var1=' + var1,
    url:'myurl',
    success:function(response){

        alert(response);
    },
    error:function(e){

    alert('Error:' +e);
    }
});
Maximilian Riegler
  • 22,720
  • 4
  • 62
  • 71
user3406754
  • 69
  • 1
  • 7
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Qantas 94 Heavy Mar 26 '14 at 12:38

2 Answers2

0

The response object contains the JSON. Use console.log instead of alert because alert only says "Object [Object]" while console.log recursively prints the whole structure of the object into the firebug console.

console.log(response)

If you want to access a value inside the response object simply do:

console.log(response["6"]); // as per your example

Or if you want to iterate through all values:

for (i in response) {
    console.log(i); // this should print 6, 7 and so on
    console.log(response[i]);
}
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
0

A simple solution will be to save the json data in a hashtable.

$(document).ready(function () {
    var hashtable = {};

Then in the success method do the following:

success:function(response, status) {
                for (i = 0; i <= n; i++) {
                    hashtable[response.d[i]] = [response.d[i].Col1];
                }
},

You can use hashtable as a single or multi-dimensional array.