1

I have a button, when event click I get return value from JSON. And then I want to display specific data from object JSON. How to implement to my code? I search in other forum, and I get this link to find object in JSON, but this is not work in my code.

This is my JSON result from Firebug:

{ "rajaongkir": {
    "query": {
      "origin": "501",
      "destination": "114",
      "weight": 1700,
      "courier": "jne"
    },
    "status": {
      "code": 200,
      "description": "OK"
    },
    "origin_details": {
      "city_id": "501",
      "province_id": "5",
      "province": "DI    Yogyakarta",
      "type": "Kota",
      "city_name": "Yogyakarta",
      "postal_code": "55222"
    },
    "destination_details": {
      "city_id": "114",
      "province_id": "1",
      "province": "Bali",
      "type": "Kota",
      "city_name    ": "Denpasar",
      "postal_code": "80227"
    },
    "results": [
      {
        "code": "jne",
        "name": "Jalur Nugraha Ekakurir (JNE)",
        "costs": [
          {
            "service": "OKE",
            "description": "Ongkos Kirim Ekonomis",
            "cost": [
              {
                "value": 42000,
                "etd": "4-5",
                "note": ""
              }
            ]
          },
          {
            "service": "REG",
            "description": "Layanan Reguler",
            "cost": [
              {
                "value": 48000,
                "etd": "2-3",
                "note": ""
              }
            ]
          },
          {
            "service": "YES",
            "description": "Yakin Esok Sampai",
            "cost": [
              {
                "value": 104000,
                "etd": "1-1",
                "note": ""
              }
            ]
          },
          {
            "service": "SPS",
            "description": "Super Speed",
            "cost": [
              {
                "value": 367000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "JTR",
            "description": "JNE Trucking",
            "cost": [
              {
                "value": 50000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "JTR250",
            "description": "JNE Trucking",
            "cost": [
              {
                "value": 1350000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "JTR<150",
            "description": "JNE Trucking",
            "cost": [
              {
                "value": 500000,
                "etd": "",
                "note": ""
              }
            ]
          },
          {
            "service": "JTR>250",
            "description": "JNE Trucking",
            "cost": [
              {
                "value": 1900000,
                "etd": "",
                "note": ""
              }
            ]
          }
        ]
      }
    ]
  }
}

And then, I just want to display this object to my div "value":48000" or in criteria service:Reg.

and this is my jQuery:

$("#hitungJNE").click(function () {  
    $.ajax({
        cache: false,
        type: 'POST',
        url: 'cost.php',
        data: {  
            isHapus : 0
        },
        dataType: 'json',
        success: function(isSuccess) 
        {
            var test = getObjects(isSuccess, 'service', 'REG'); 
            $('#myDiv').text(test); 
           //i want set this value ex: $('#myDiv').text(48000); 
        }
    });
}); 

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

I'm read from this link use jQuery's find() on JSON object but not working for me.

Community
  • 1
  • 1
achmad darmawan
  • 125
  • 1
  • 1
  • 9
  • *"not working"* is not a proper problem statement that tells anyone anything of value. What does it return? Are errors thrown? Does the ajax even succeed? Shouldn't you be looking in the `results` array? Take a few minutes to read through [ask] – charlietfl Dec 17 '16 at 19:13
  • I had been very dizzy, please give me your answer to fix my script so that the results I want answered. thank you – achmad darmawan Dec 18 '16 at 01:26
  • not entirely clear what exactly it is you are wanting to do and what the expected results are – charlietfl Dec 18 '16 at 01:27
  • i just wanna display this object to my div "value":48000" or in criteria service:Reg. – achmad darmawan Dec 18 '16 at 01:44
  • `please give me your answer to fix my script` - if this is a "please finish my work for me" question, then it is too broad for Stack Overflow. Are you able to narrow the problem down a bit? – halfer Dec 18 '16 at 09:04
  • thanks, my case is close with this link : http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php – achmad darmawan Dec 20 '16 at 04:11

0 Answers0