0

i need help in parsing a json array with javascript .

var data={
  "root_name": {
      "0": "400",
      "1": "test coupon by ashutosh",
      "2": "90",
      "3": "1",
      "4": "2",
      "5": "",
      "6": "test coupon by ashutosh test coupon by ashutosh test coupon by ashutoshundefined",
      "7": "",
      "8": "2",
      "9": "1",
      "10": "50",
      "11": "0",
      "12": "0",
      "13": "0",
      "14": "",
      "15": "0",
      "16": "0",
      "17": "1",
      "18": "0",
      "19": "Test Store",
      "20": "0",
      "21": "test coupon by ashutosh test coupon by ashutosh",
      "22": "250002",
      "23": "0",
      "24": "1",
      "25": "2017-03-14 00:06:07",
      "26": "",
      "27": "",
      "id": "400",
      "title": "test coupon by ashutosh",
      "user_id": "90",
      "category_id": "1",
      "subcategory_id": "2",
      "image": "",
      "description": "test coupon by ashutosh test coupon by ashutosh test coupon by ashutoshundefined",
      "tags": "",
      "duration": "2",
      "charges": "1",
      "fixed_rate": "50",
      "hour_rate": "0",
      "buy_one": "0",
      "straight_price": "0",
      "straight_off": "",
      "was_price": "0",
      "now_price": "0",
      "coupan_type": "1",
      "no_of_view": "0",
      "business_name": "Test Store",
      "no_like": "0",
      "advice": "test coupon by ashutosh test coupon by ashutosh",
      "zipcode": "250002",
      "chk_add": "0",
      "status": "1",
      "post_date": "2017-03-14 00:06:07",
      "lat": "",
      "lon": ""
   }
}

this is how i'm trying to parse the above json array with javascript

var jsonData = JSON.parse(data);
alert(jsonData.root_name.length)
for (var i = 0; i < jsonData.root_name.length; i++) {
    var counter = jsonData.root_name[i];
    alert(counter.id);
}

Here i'm getting jsonData.root_name.length as undefined also unable to parse the array .

Can anyone please guide how to make this work?

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
phpdev
  • 89
  • 2
  • 10
  • Try JSON.parse(JSON.stringify(data)) – Vaibhav Patil Mar 14 '17 at 06:09
  • 1
    The `root_name` is an object and has no `length`. – E. Sundin Mar 14 '17 at 06:10
  • 4
    `data` is already an object. You cannot call `JSON.parse` on it. There is no array either. – Felix Kling Mar 14 '17 at 06:11
  • its not array its JSON object, you can directly loop though this.. – Abhinav Mar 14 '17 at 06:14
  • @FelixKling thanks for your help , can you please let me know how can i retrieve values i want to retrieve id and title . – phpdev Mar 14 '17 at 06:32
  • Have a look at the duplicate question. – Felix Kling Mar 14 '17 at 06:33
  • @FelixKling sorry but still struggling to get the values . – phpdev Mar 14 '17 at 06:56
  • `data.root_name.id` – Felix Kling Mar 14 '17 at 07:17
  • @FelixKling alert(data.root_name.id); doesn't yield any result :( . – phpdev Mar 14 '17 at 07:29
  • Then your actual code / data is different, since it works with what you posted: https://jsfiddle.net/3q60xu0a/ . If `data` is actually a string containing JSON then you have to parse it first of course (as explained in the duplicate). But in your example, `data` is an object. I really recommend to spend some time reading the duplicate and make sure you understand it. It contains everything you need to know. – Felix Kling Mar 14 '17 at 07:31
  • i'm really thankful for your help . what i'm doing i passing value with ajax and the array response is what i'm getting in success . now i want to retrieve value of id . from the returned values in success . I'm sure this is something very easy for you if you can guide me what i'm doign wrong . i'll be really thankful . – phpdev Mar 14 '17 at 07:37

1 Answers1

0

you can directly loop through this data..

$.each(data.root_name, function(i){
            console.log(data.root_name[i]);
        })

loop into JS

for(i in data.root_name){
                console.log(data.root_name[i]);
            }
Abhinav
  • 1,202
  • 1
  • 8
  • 12