Search code examples
javascriptjqueryjsonintel-xdk

json value javascript XDK


I'd like to do a little REST API in my Intel XDK application. My PHP file returns values like :

{"Name1":12,"Name2":34,"Name3":67}

Now I can access to my values with this code, on a onclick event :

  var json_string = request.responseText;
  var json = JSON.parse(json_string);
  intel.xdk.notification.alert(json.Name1, "Name 1");

It works, I have the first value for Name1 : 12.

Now I want to add an input text to insert the value I want. Instead of using "json.Name1", I want to do it dynamically. So I tried to add an input text :

<input id="test" type="text" placeholder="Placeholder">

And getting the value :

var value = $("#test").val();

Finally, I tried :

intel.xdk.notification.alert(json.value, "Name 1");

But it doesn't work. How can I add a value from an input to have JSON value? I would like to obtain the value of Name1, Name2 or Name3 if I write it in the input.

Thanks!


Solution

  • To access a key in json with the name from an input...

    var json = {"Name1":12,"Name2":34,"Name3":67};
    
    alert(json[document.getElementById('test').value]);
    <input id="test" type="text" placeholder="Placeholder" value="Name1" />

    JQuery...

    json[$("#test").val()]