Extracting a string from a response text - javascript

I'm trying to extract just the response from the following response
"{"Message":"Looks like you need to login"}"
I tried to stringyfy it as follows
var response = "{"Message":"Looks like you need to login"}";
var json_response = JSON.stringify(response);
But my response ends up looking something like this.
"{\"Message\":\"Looks like you need to login\"}"
Any ideas on why this is happening? and how I can extract just the message by doing something like
json_response.Message perhaps?

You need to use JSON.parse():
var str = "{\"Message\":\"Looks like you need to login\"}";
var json = JSON.parse(str);
console.log(json.Message);

You need to use parse() method of json which is very useful. So keep using that it is very light weighted.
Here is my answer :
var myString = "{\"Message\":\"Looks like you need to login\"}";
var parsedJson = JSON.parse(myString);
alert(parsedJson.Message);

Try this:
var response = { Message: "Looks like you need to login" } ;
var json_response = JSON.stringify( response ) ;

Related

how to make json key value pair using two var

I have two var's
var a = result.master_id;
var b = result.session_name;
i want to create a json string to pass to ajax. i tried this
var userData = {'pid':a,'session' :b};
i'm storing this userData in a sessionStorage and sending to ajax . but i'm getting pid undefined error.
is this the right way to do this?
any help is appreciated.
regards,
newbie
You are using the right code to store into variable. Try to console variable to identify.
Example code given below:
let result = {
master_id: 1,
session_name: "Hardik"
}
var a = result.master_id;
var b = result.session_name;
var userData = {'pid':a,'session' :b};
console.log(userData);

Founding a values in parsed multidimensional array in javascript

I've got this data serialized by JSON to string:
var json = '[{"data":{"id":2,"gid":3,"name":"Travis","surname":"Stewart","skin":0}},{"data":{"id":3,"gid":3,"name":"Ziutek","surname":"Stewart","skin":0}}]';
And now im going to parse this back to object, like:
var charData = JSON.parse(json);
How can i get data from this for each datas?
for(var char in charData.data) {
console.log(char.id);
}
This wasn't work... Any other solutions?
Okay. Got it! Here's the code how can i get this:
var json = '[{"data":{"id":2,"gid":3,"name":"Travis","surname":"Stewart","skin":0}},{"data":{"id":3,"gid":3,"name":"Ziutek","surname":"Stewart","skin":0}}]';
var charData = JSON.parse(json);
for(var char in charData) {
console.log(charData[char].data.name);
}
Thanks a lot for all! :) SOLVED.
you have more then one Data object , so you need to do a forOf To your chardata not to charData.data :
for(var char in charData) {
console.log(charData[char].data.id);
console.log(charData[char].data.name);
....
}

How to get json value by string?

I receive the following string from the server response:
var jsonData = '[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"},{"firstName":"Thomas","lastName":"Carter"}]';
I see some jquery plugins can predefine the keys they want
like: index:"firstName", and they get a ul like
<li>Bill</li>
<li>George</li>
<li>Thomas</li>
If index:"lastName", they get a ul like
<li>Gates</li>
<li>Bush</li>
<li>Carter</li>
The only way I know how to parse a json format string is:
var object = JSON.parse(jsonData);
var firstName = object[i].firstName;
var lastName= object[i].lastName;
The plugin pass the index like a parameter
function f(index) {
return object[i].index;
}
How can they achieve this?
Thanks for helping!
You can access object properties with square brackets. JS objects work like arrays in this regard.
var objects = JSON.parse(jsonData),
key = "firstName";
objects.forEach(function (obj) {
var value = obj[key];
// ...
});

put string as javascript object

i've the following object and I need to put the "string",how should I put the value of name
{"name":{"_parent":["/test"]}}
inside,I try with " which doesnt work for me ,any idea ?
var file = {
"name" : "{"name":{"_parent":["/test"]}}"
update
I cannot use the jsonParse or stringify as I need to put it hardcoded
Your question is a bit unclear but I think you want to do this
The reason why JSON.parse() is not working for you is because you are missing the single quotes.
var yourString = '{"_parent":["/test"]}'
var file = '{"name" : {"name":{"_parent":["/test"]}}}'
var obj = JSON.parse(file);
obj.name.name = yourString;
var backToString = JSON.stringify(obj);
Use this :
JSON.parse(file);
For more information you could look in this site :
http://www.w3schools.com/js/js_json.asp

How to read a javascript object

I am using from Microsoft the
Live Connect Developer Center
It returns this type of variable for a contact but I don't know of a simple way to read it, would perform split on it but do not know how to read this object:
{"id":"contact.0d3d6bf0000000000000000000000000", "first_name":"William", "last_name":"Shakespeare", "name":"William Shakespeare", "gender":null, "is_friend":false, "is_favorite":false, "user_id":"2ae098749083cb3d", "email_hashes":["a790b818acfdef744a23bef534dfd9a4a53aa834250bdfe55f6874543129daa6"], "updated_time":"2012-10-04T19:23:34+0000"}
I'd need to access name and email_hashes with what's inside of it:
a790b818acfdef744a23bef534dfd9a4a53aa834250bdfe55f6874543129daa6 - without the brackets.
Just don't know how to read this kind of object.
JSON.parse() is specifically designed to take a string in JSON format and produce a JavaScript object, from which you can then access properties.
That looks like JSON. If you're using jQuery, you could do something like this:
var jsonData = $.parseJSON('{"id":"contact..."}');
alert('name: ' + jsonData.id);
See the docs for more usage examples: http://api.jquery.com/jQuery.parseJSON/
The response you're receiving is a key/value pair. You can access any value with the key
obj[key] // value
or
obj.key // value
if
var x = {"id":"contact.0d3d6bf0000000000000000000000000", "first_name":"William", "last_name":"Shakespeare", "name":"William Shakespeare", "gender":null, "is_friend":false, "is_favorite":false, "user_id":"2ae098749083cb3d", "email_hashes":["a790b818acfdef744a23bef534dfd9a4a53aa834250bdfe55f6874543129daa6"], "updated_time":"2012-10-04T19:23:34+0000"}
then
x.email_hashes
returns
["a790b818acfdef744a23bef534dfd9a4a53aa834250bdfe55f6874543129daa6"]
and
x.email_hashes[0]
returns
"a790b818acfdef744a23bef534dfd9a4a53aa834250bdfe55f6874543129daa6"
When you get your variable with the JSON, do this:
var stringData = {}, // Incoming data
data = JSON.parse(stringData);
Then, you can access the variables like this:
var id = data.id,
firstName = data.first_name;
To access array values, do this:
var emailHashes = data.email_hashes;
if (emailHashes.length > 0) {
var i = 0;
for (; i < emailHashes.length; i++) {
// perform some action on them.
}
}

Categories

Resources