get json value

On error I am returning json results.
var text = '{"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."],"model.Password":["The Password field is required."]}}';



var mytext = {"Message":"The request is invalid.","ModelState":{"":["Passwords must have at least one non letter or digit character. Passwords must have at least one lowercase ('a'-'z'). Passwords must have at least one uppercase ('A'-'Z')."]}}

Open in new window


function (error, status) {

Open in new window


I tried json.parse
LVL 9
Seven priceFull StackAsked:
Who is Participating?
 
Chris StanyonCommented:
Right.

Have a look at this:

var result = '{"Message":"The request is invalid.","ModelState":{"model.Email":["The Email field is required."],"model.Password":["The Password field is required."]}}';

var obj = jQuery.parseJSON( result );
console.log( obj.ModelState["model.Email"][0] );
console.log( obj.ModelState["model.Password"][0] );

Open in new window


Working Demo: http://jsfiddle.net/ChrisStanyon/7r3dwg9b/
0
 
Pravin AsarPrincipal Systems EngineerCommented:
Your json is valid. (Use http://jsonlint.com/)

Looks like your mytext assignment has missing quote (')

When you add these quotes, you have to properly escape occurence of quotes within string.

For example:

mytext ='{"Message":"The request is invalid.","ModelState":{"Warning":["Passwords must have at least one non letter or digit character. Passwords must have at least one lowercase (\'a\'-\'z\'). Passwords must have at least one uppercase (\'A\'-\'Z\')."]}}';

 alert (mytext);
0
 
Pravin AsarPrincipal Systems EngineerCommented:
I noticed in your string, the missing element name.

"ModelState":{""


I assigned "Warning" as name.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Seven priceFull StackAuthor Commented:
ok I know it is valid I am trying to return the value in a alert box.
0
 
Chris StanyonCommented:
What is 'function (error, status) {'

You haven't really given us a lot to go on, or indicated where you have a problem.

My guess here is that you are trying to make an AJAX request and expecting the error handler to notice when your username or password aren't valid.

If that's the case, the jQuery request will only receive the JSON data back in the success handler, not the error handler. The error handler only fires if the AJAX request failed.
0
 
Seven priceFull StackAuthor Commented:
Yes I know that, I set up a demo on jfiddler. being lazy today. ok
0
 
Chris StanyonCommented:
OK - In that case, do you want to give us some clue as to what your problem is, or at least what you're trying to do?

You've given us exactly 2 variable definitions and 1 function declaration. No errors, no questions, no idea as to what you need. Work with us and we can help you.
0
 
Seven priceFull StackAuthor Commented:
http://jsfiddle.net/sevensnake/vqbvnvh2/2/

Ok I want to get the value from result  modelstate and result2 model state. either example will work.
0
 
Seven priceFull StackAuthor Commented:
I kind of figured to use a object. good job. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.