Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from json string using fields

I have a json string. I want to get the values from that string using the field name. Please help me to get this. This is my json string format.

[
    {
        "FLD_ID": 1,
        "FLD_DATE": "17-02-2014 04:57:19 PM"
        "FLD_USER_NAME": "DAFEDA",
        "FLD_USER_EMAIL": "[email protected]",
        "FLD_USER_PASS": "test"
    }
]
like image 362
user3085540 Avatar asked Aug 16 '26 17:08

user3085540


2 Answers

I'm not really sure what the question is but how about

// assuming str is your JSON string

var obj = JSON.parse(str); // parse the string into an object

var firstObj = obj[0]; // get the first (and only) object out of the array

var fld_id = firstObj.FLD_ID; // you can access properties by name like this

var fld_date = firstObj['FLD_DATE']; // or like this
like image 125
Phil Avatar answered Aug 19 '26 09:08

Phil


your JSON was invalid. I fixed it for you.

[{"FLD_ID":1,"FLD_DATE":"17-02-2014 04:57:19 PM", "FLD_USER_NAME":"DAFEDA","FLD_USER_EMAIL":"[email protected]","FLD_USER_PASS":"test"}]

here's a working example of how to alert the FLD_ID

<script>
var json = [{"FLD_ID":1,"FLD_DATE":"17-02-2014 04:57:19 PM", "FLD_USER_NAME":"DAFEDA","FLD_USER_EMAIL":"[email protected]","FLD_USER_PASS":"test"}];
alert(json[0].FLD_ID);
</script>

By the way, this is an array with 1 JSON object, which is why you must reference the index, 0 in this case.

like image 44
Zack Avatar answered Aug 19 '26 09:08

Zack