2

I have an ajaxpost that returns a response in form of json and I want to show the message

{ "TEXT" : "Please fix it. there might be otehr reason, and need to address it, or call, incorrect username/password-username/port?. " } 

How can I get the value of json? I used the following but Message is undefined, What are the other ways of getting json object

var TEXT = text.resp;
  • 2
    If *"messageObj.responseText equals to the json above"* then `Message` cannot be `undefined`, because you obviously asssign `messageObj.responseText` to `Message`. Maybe you are trying to access `Message` before the assignment takes place? Please provide more information about your problem. – Felix Kling Sep 16 '13 at 11:46
  • Please post a more complete example of your code. The response you get is indeed JSON and you'll have to parse it, but your problem seems to be before that step. – Felix Kling Sep 16 '13 at 11:54
  • @user2781855: Did my solution worked for you? – Jitendra Pancholi Sep 16 '13 at 11:59
  • Try using the suggestion from Andy E.: http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – Patrick G Sep 16 '13 at 12:25

2 Answers2

1

Include this library and below code to parse your json

var json = $.parseJSON(obj.responseText);
var msg = json.message;

You can also try below code

var json = JSON.parse(obj.responseText);
var msg = json.message;
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
  • Ah... maybe it should be explicitly stated that this answer depends on an external library. But including jQuery to parse JSON is overkill. Just use `JSON.parse`. – Felix Kling Sep 16 '13 at 11:53
  • you must have to include jquery library in order to use it. download it from http://code.jquery.com/jquery-1.10.2.js – Jitendra Pancholi Sep 16 '13 at 13:03
  • Thanks, ya I did that, I am thinking maybe this format is not json, but as I checked it should be – user2781855 Sep 16 '13 at 13:16
0

You should try to parse the object and then access it with json["object"] instead of the dot.

Justin D.
  • 4,946
  • 5
  • 36
  • 69
  • Why do you suggest bracket notation instead of dot notation? What is `json` in your example? – Felix Kling Sep 16 '13 at 11:52
  • I have found that sometimes, though dot notation is not working, bracket notation is. json is obviously the parsed json object. – Justin D. Sep 16 '13 at 14:56
  • [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). It's true that for dot notation, the property name must be a valid identifier name. So e.g. `foo['bar-']` works but `foo.bar-` wouldn't. In your example `object` is a valid identifier though, so `json.object` would work fine. There is no property name in the OP's JSON example that would require bracket notation (`message` is a valid identifier name as well). That's why I was wondering why you are suggesting it and why you used `"object"` as example. It won't solve the OP's problem. – Felix Kling Sep 16 '13 at 14:59
  • I am well aware there is no such thing as a json object, I was refering to the **parsed** json which is an object. – Justin D. Sep 16 '13 at 16:36