1

I have label in html with Id employeename and I want to set above message employee name is must to html label with Id employeename how to do it using JavaScript or jquery

{"fieldErrors":[{"field":"employeename","message":"NotEmpty.employee.employeename"}]}
The above json I am getting from Ajax respons as 
error:(status,jhxr,errorthrown)
{

}

1 Answer 1

5

You can use JSON.parse() method to do that :

var str =  '{"fieldErrors":[{"field":"employeename","message":"NotEmpty.employee.employeename"}]}';
var parsedJson = JSON.parse(str);

Once it is parsed, You can refer to the keys as an object, like , if you want the "field" inside the object , just say,

var field = parsedJson.fieldErrors[0].field;

Similarly, for any field inside it.

Sign up to request clarification or add additional context in comments.

3 Comments

its should be var str :)
It will then return it as an object. to get e.g. the message variable, do it easily with parsedJson.fieldErrors[0].message
@Deep Changed it. But string is also fine. Javascript is case-sensitive :). But, Just changed it to avoid confusion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.