Simon
Simon

Reputation: 205

Get value from a JSON

I have a JSON that looks like this, it comes from a knockout array and has been converted. I simply want to content this to a single object to send to a service.

[{\"NotePadID\":-1,\"UserID\":\"NIGOV\\\\\\\\dard-lytts\",\"DateInput\":\"08/28/2014\",\"CategoryID\":6,\"SubCategoryName\":\"Active Farmer\",\"DateCreated\":\"08/28/2014\",\"Note\":\"test\"}]

This comes as an array how would I get the first, ie

  var test = jsonConverted[0]['CategoryID'];

Always returns undifined. What I actually want to do is just pass a single object to my ajax, should be simple but not working.

Upvotes: 0

Views: 71

Answers (2)

gihandilanka
gihandilanka

Reputation: 615

you can try this and follow the "Demo"

var stringiFiedArray = "[{\"NotePadID\":-1,\"UserID\":\"NIGOV\\\\\\\\dard-lytts\",\"DateInput\":\"08/28/2014\",\"CategoryID\":6,\"SubCategoryName\":\"Active Farmer\",\"DateCreated\":\"08/28/2014\",\"Note\":\"test\"}]";

var javasArrar = JSON.parse(stringiFiedArray);
alert(javasArrar[0]["CategoryID"]);

you can click here to see Demo

Upvotes: 0

PiyusG
PiyusG

Reputation: 1157

using javascript you can do it like

var a = '[{\"NotePadID\":-1,\"UserID\":\"NIGOV\\\\\\\\dard-lytts\",\"DateInput\":\"08/28/2014\",\"CategoryID\":6,\"SubCategoryName\":\"Active Farmer\",\"DateCreated\":\"08/28/2014\",\"Note\":\"test\"}]'
return JSON.parse(a)[0]['CategoryID']

Upvotes: 1

Related Questions