1

I have an object and I want to show its content.

{"text": "active    user
          active  user213123
          idle      user234234234
          loggedout  userafdadf"
},

I did Response = message.split(":")[1]; and I got:

"active    user
          active  user213123
          idle      user234234234
          loggedout  userafdadf"
},

then: var value = message.split("}")[0]; and I got:

"active    user
          active  user213123
          idle      user234234234
          loggedout  userafdadf"

So now how can I get rid of "" so I can have the raw valu of text? and beside that is there any other way to retrive a value of the object? because this way seems crazy..

I put the whole object that I am fetching the "text" from that here: (thats what chrome debugger is showing after console log)

{
getResponseHeader: function ( key ){}
pipe: function ( /* STDone, STFail, STProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 4

    TextContent: "{"text": "active    user
                  active  user213123
                  idle      user234234234
                  loggedout  userafdadf"}"

Suppose they are in message variable and then I did Response = message.TextContent; and after console.log I received:

 TextContent: "{"text": "active    user
              active  user213123
              idle      user234234234
              loggedout  userafdadf"}"

Now I want the raw value of text

5
  • As it is json Object use it like value get from json Commented Sep 11, 2013 at 4:58
  • Where does message come from? Why does it have a trailing comma? Commented Sep 11, 2013 at 5:01
  • Your question is a little confusing. It sounds like your asking how do you get the values of user, active, idle and loggedout. but your question asks how do you get the value of text which is just text as it's not a correctly formatted to get the individual values Commented Sep 11, 2013 at 5:07
  • The snippet you post is clearly part of a larger string. You're handling this snippet as a string, when in fact the whole thing can probably be treated as an object. Post the complete object and expect a straightforward answer. Commented Sep 11, 2013 at 5:15
  • 1
    Please leave some comments in order to help folks to guide you, or at least, vote for the most reliable answers, then, mark the best answer as accepted. Commented Sep 11, 2013 at 6:15

8 Answers 8

1

Why not Just replace it?

myString = myString.replace(/["]/g,'')
console.log(myString);
//or
alert(myString);

In case you want to see it live http://jsfiddle.net/PwGcr/

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

Comments

0
var myObj = {text: "active    user
             active  user213123
             idle      user234234234
             loggedout  userafdadf" 
            };

var myText = myObj.text

Just access it like a property on the object

Comments

0
 var jsonObj={"text": "active  user active  user213123 idle user234234234 loggedout  userafdadf"};
 alert('json obj '+jsonObj["text"]);

Comments

0

It's a JSON string. Instead of trying string manipulation on it - parse it into JSON object and use Object's property e.g.

 var obj = JSON.parse(message);
 alert(obj.text);

2 Comments

I get undefined when I try this
Where're u getting undefined? Here's a demo of this code working: jsfiddle.net/aB8bL/1
0

You can use following function to remove all characters from your string.

String.prototype.replaceAll = function(token, newToken, ignoreCase) {
  var _token;
  var str = this + "";
  var i = -1;

  if (typeof token === "string") {

    if (ignoreCase) {

      _token = token.toLowerCase();

      while ((i = str.toLowerCase().indexOf(token, i >= 0 ? i + newToken.length : 0)) !== -1) {
        str = str.substring(0, i) + newToken + str.substring(i + token.length);
      }

    } else {
      return this.split(token).join(newToken);
    }

  }
  return str;
};

Comments

0

I guess you are dealing with some JSON string, if so, you have to turn it into a javascript structure in order to be able to query it :

function parseJSON(json) {
    return (new Function('return ' + json + ';'))();
}
var o = parseJSON('{"test":"some content"}');
o.test // "some content"

However, in your example, there is a comma at the end of the string, this may throw an error. Make sure to remove it before parsing.

Note that there are several ways to parse JSON, this one should work in all modern browsers : https://stackoverflow.com/a/18733016/1636522. Check this link to see which browsers currently support JSON.parse().

Comments

0
TextContent: "{"text": "active    user
          active  user213123
          idle      user234234234
          loggedout  userafdadf"}"

Here the TextContent property is a string, not a JSON object. You should use JSON.parse(message.TextContent) to convert string to JSON object.

var jsonMessage = JSON.parse(message.TextContent);
alert(jsonMessage.text); // will alert the value of **text** property.

Refer http://jsfiddle.net/5MtNf/1/.

2 Comments

When I try that I recieved: undefined
var myObj = {"text": "You test message" }; alert(myObj.text); It should work, Refer jsfiddle.net/5MtNf
0

To omit " from beginning of string you can use:

string.substr(1)

Take a look at this link for working with strings, but I suggest you for working with object look at this.

First way is bad coding style and not correct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.