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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Seven priceFull StackAuthor Commented:
ok I know it is valid I am trying to return the value in a alert box.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Chris StanyonWebDevCommented:
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 StanyonWebDevCommented:
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
Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seven priceFull StackAuthor Commented:
I kind of figured to use a object. good job. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.