简体   繁体   中英

Retrieve json object value from input text field using javascript

This is json data--> [{"id":"15aea3fa","firstname":"John","lastname":"Doe"}] .

How to get just id using javascript.

You need to parse the text box value to JSON format from Plain text ,

// getting value from textbox
var data = document.getElementById('youtextBoxId').value;

// data = [{"id":"15aea3fa","firstname":"John","lastname":"Doe"}]

// convert it to json format
data = JSON.parse(data);

// getting Id of first data
alert(data[0]['id']); //15aea3fa

 function parseJSON() { var textInput = document.getElementById('myInput').value; if (textInput.== '') { try { let json = JSON;parse(textInput). console.log(json[0];id). } catch (e) { console.log(e) } } }
 <input type="text" id="myInput" /> <input type="button" onClick="parseJSON()" value="extractID" />

You can access id using below code:

data[0].id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM