how can i parse json object using javascript..? - javascript

var Response = $.parseJSON(data.d);
this code is not working..
my json object is
[{"ItemId":1,"ItemName":"Sizzler"},{"ItemId":2,"ItemName":"Starter"},{"ItemId":3,"ItemName":"Salad"}]
How can i parse this?

got the solution. I have included 2 jquery and because of their clash my code was not working..
my working code is :
var jsonp = data.d;
var lang = '';
var obj = $.parseJSON(jsonp);
$.each(obj, function() {
lang += this['ItemId'] + " ";
});
alert(lang);

Related

Fetch data via GS - response code 200 but data always "undefined"

I try to fetch the data given by https://api.llama.fi/charts/Ethereum
It looks like
[{"date":"1546560000","totalLiquidityUSD":273845651.27077854},{"date":"1546646400","totalLiquidityUSD":288674544.41292274},{"date":"1546732800","totalLiquidityUSD":297321259.6930144},{"date":"1546819200","totalLiquidityUSD":286168221.103729},{"date":"1546905600","totalLiquidityUSD":285073686.76345384}, ...
when I open it in chrome.
In python with urllib.request.urlopen() it works fine.
Here in Google Sheets I try
function myFunction() {
var data = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getResponseCode()
var data2 = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText()
console.log(UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText())
}
Here data = 200, but data2 = undefined. I think it is a newbie question, but I am unfamiliar with JS or GS.
Thanks for your help!
Best,
Dominik
You can access the data as JSON object by parsing the ContentText as JSON, then you can iterate the data with a for or use it in any other way you need.
function myFunction() {
var response = UrlFetchApp.fetch("https://api.llama.fi/charts/Ethereum").getContentText()
var data = JSON.parse(response);
for(const d of data) {
console.log("DATE: " + d.date)
console.log("LIQUIDITY: " + d.totalLiquidityUSD)
}
}

Json result get values

from controller Json is returned and in function i get an object which contains
{
"readyState":4,
"responseText":"{\"Success\":0,\"Failed\":0}",
"responseJSON":{
"Success":0,
"Failed":0
},
"status":200,
"statusText":"OK"
}
How can I take Success and Failed values?
data.Successand JSON.parse(data) is not working
You dont need to parse that because that IS already an object:
var obj = {"readyState":4,"responseText":"{\"Success\":0,\"Failed\":0}","responseJSON":{"Success":0,"Failed":0},"status":200,"statusText":"OK"};
var failed = obj.responseJSON.Failed;
var success = obj.responseJSON.Success;
var json_data = '{"readyState":4,"responseText":"{\"Success\":0,\"Failed\":0}",
"responseJSON":{"Success":0,"Failed":0},"status":200,"statusText":"OK"}';
var obj = JSON.parse(json_data);
alert(obj.responseJSON.Success); // for success that in responseJSON
alert(obj.responseJSON.Failed);
Thanks :)

How can i change JSON object data with Greasemonkey

A site that I'm using contains a JavaScript file, inside the file it contains a code with JSON and goes like this:
$.getJSON(API_URL + getSubdomain() + "/values");
The data file contains the code:
{"has_clicked_all":false,"has_clicked_already":false,"buttons":
[{"id":3922,"name":"button3992","has_clicked":false},
{"id":4613,"name":"button4613","has_clicked":false},
{"id":4339,"name":"button4339","has_clicked":false},
{"id":4340,"name":"button4340","has_clicked":false},
{"id":4341,"name":"button4341","has_clicked":false},
{"id":4622,"name":"button4622","has_clicked":false},
{"id":4623,"name":"button4623","has_clicked":false},
{"id":4828,"name":"button4828","has_clicked":false},
{"id":4829,"name":"button4829","has_clicked":false},
{"id":4861,"name":"button4861","has_clicked":false}]}
What I'm wanting to do is change all the value's to true without clicking on all of the buttons. I've tried doing a lot of research and spent 4 hours figuring out how I could do this and me being very new to all this just leaves me stuck...
I just need someone to give me a hand it would be greatly appreciated! Thankyou
Parse the json , modify the data , then re-encode to json
javascript:
var data = JSON.parse(json);
for(i = 0; i < data.buttons.length; i++){
data.buttons[i].has_clicked = true;
}
data.has_clicked_all = true;
data = JSON.stringify(data);
console.log(data);
demo
jQuery :
(function() {
$.getJSON(API_URL + getSubdomain() + "/values", function(data) {
data.has_clicked_all = true;
$.each( data.buttons, function( i, item) {
data.buttons[i].has_clicked = true;
});
data = JSON.stringify(data);
alert(data);
});
})();

How to remove template markup if data is missing in Handlebars?

How would one remove all the template markup from a HTML snippet if the data wasn't present.
var $target = $(target);
var dataString = // json from feed ! Sometimes fails
if( typeof dataString !== "undefined") {
var data = $.parseJSON(dataString);
var template = Handlebars.compile($target.html());
var html = template(integrationData);
$target.html(html);
} else {
// What goes here?
// I am making up "stripTags"
Handlebars.stripTags($target.html());
}
Basically I want to automatically strip out all the {{ and }} as a failsafe.
Handlebars will ignore any {{whatever}} if the object does not exist and they will be removed from the markup.
If your feed fails default to an empty object:
var $target = $(target);
var dataString = // json from feed ! Sometimes fails
var data = {};
if( typeof dataString !== "undefined") {
data = $.parseJSON(dataString);
}
var template = Handlebars.compile($target.html());
var html = template(data);
$target.html(html);
Hope that helps?

Need help turning a jQuery object back into a string

(i am new. please bear with me.) I have a jquery object that I need to convert back to html to use. What I am doing is using jQuery's get to get the HTML DOM of a local file. The data returned is then made into an object and I perform some tweaks on it (like changing hrefs etc.)
$.get(imagePath + "bloghome.aspx", function (data) {
var pageHtml = $(data);
pageHtml.find('a').each(function () {
var longHref = $(this).attr('href');
var tmp = longHref.lastIndexOf('ID=');
var id = longHref.substring(tmp + 3) + '.htm';
var newHref = imagePath.concat(id);
$(this).attr('href', newHref);
});
});
the object is created in the second line and then i change the hrefs. What I need now is to turn that object back into a string so that I can write it to a file.
I am using PhoneGap but any help would be appreciated as I am stumped
You can do this way using pageHtml[0].outerHTML:
$.get(imagePath + "bloghome.aspx", function (data) {
var pageHtml = $(data);
pageHtml.find('a').each(function () {
var longHref = $(this).attr('href');
var tmp = longHref.lastIndexOf('ID=');
var id = longHref.substring(tmp + 3) + '.htm';
var newHref = imagePath.concat(id);
$(this).attr('href', newHref);
var htmlString = pageHtml[0].outerHTML; //<-- Here
});
});
from http://api.jquery.com/html/
console.log(pageHtml.html());
Can you just do
pageHtml.html();
?
EDIT: Using this will only give you the contents inside the main wrapping element, if you want the entire thing, you can use:
pageHtml[0].outerHTML;
instead.

Categories