Rendering JSON using javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
We have a link at the end of which is a lot of JSON. How can we turn the contents of the link into a string, and then render that JSON as objects?
EDIT
We have decided to use other methods, but thank you for your help. We have accepted the answer we found most useful

This is the only answer I can give with the amount of info you provided
document.body.innerHTML = jsonData.toString();
EDIT: alternativly if its a regular JS object you would need to do:
document.body.innerHTML = JSON.stringify(jsonData);
I hope this helps

You could try something like that to show complex JSON to a user, with indent:
var pre = document.createElement("pre");
var textNode = document.createTextNode(JSON.stringify(someObject, null, 4));
pre.appendChild(textNode);
document.body.appendChild(pre);

Possible Duplicate of :Rendering json using jquery.
or try var myJSONObject = JSON.parse(myJSONString);
var jsonString = JSON.stringify(myJsonObject);

Try this way,,
document.body.innerHTML = JSON.stringify(jsondata);
This may help you

Related

How do I remove double quote "" from JSON Value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am doing my Javascript. And just imported JSON and printed it out with JSON.stringfy
I am using that site because the JSON contains the URL which I want to link.
I am not getting what I really need as the output.
description": "www.site.com"
This is what I get in my console:
"www.site.com"
This is what I actually want, and remove the quotes.
www.site.com
Please help!
Am not sure what your requirement is but you can do this.
Try Replace function;
const obj = {description: "www.site.com"};
JSON.stringify(obj.description).replace(/"/g,"");
var someStr = 'He said "Hello, my name is Foo"';
console.log(someStr.replace(/['"]+/g, ''));
That should do the trick... (if your goal is to replace all double quotes).

Javascript: How to parse data from this string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have the following string returned from a server:
{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
I want to extract data:image/png;base64,iVBORw0KGgoAAErkJggg== (the base64DataURL part) from it. What's the best way to do this with Javascript?
Please note that I am asking how to parse values from JSON. This is not the same as this question, which asks how to extract fields from nested objects and arrays.
What I've tried: I have tried
JSON.parse(<string>).href.split('base64DataUrl":"')[1].split('"')[0]
which yields the right answer, but I'm hoping for a more concise solution.
This is JSON, but the part you're interested in isn't exposed as part of the object. So I would suggest that you just consider it a string and use a string-parsing method, like a regular expression.
const string = `{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}`;
const dataURL = /"base64DataUrl":"(.*?(?="))/gm.exec(string)[1];
console.log(dataURL);
Since the server is not returning valid JSON, you will have to parse it with indexOf and substring.
let y = stringFromServer;
let a = y.indexOf('data:');
let b = y.indexOf('\"}',a);
let dataUrl = y.substring(a,b);
Here is my try
let sample = {"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
let splitter = sample.href.split("<-")
console.log(JSON.parse(splitter[2].substring(0, splitter[2].length - 1))["base64DataUrl"]
)

How to pass array from php to javascript using smarty 3? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In php file
When I code like this
$names = ["jim", "lucy"];
$smarty->assign('names', $names);
In javascript,
var arr = {$names};
Smarty will parse the {$names} as
...&quot... .
What should I do to avoid this? I want Smarty parse the array as
var arr = ['jim', 'lucy'];
In php:
$names = ['jim', 'lucy'];
$smarty->assign('names', $names);
In javascript,
var arr = {$names|json_encode};
Notice:
If you changed your smarty default_modifiers to array('escape:"html"'), use
var arr = {$names|json_encode nofilter};
to make everything work.
Good luck.

Find the string is XML or JSON or String using angular 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to find my string is XML or JSON object or string, based on this response I will make the string looks pretty.
So for that, I need to identify what kind of object. Can anyone please help me to find a way to achieve this?
Thanks in advance.
try like this
function GetInputType(response) {
try {
//try to parse via json
a = JSON.parse(response);
return 'json type';
} catch(e) {
//try xml parsing
let parser = new DOMParser;
var xmlDoc = parser.parseFromString(response,"application/xml");
if(xmlDoc.documentElement.nodeName == "parsererror")
return 'string type';
else
return 'xml type';
}
}

Need to replace numbers between [...] with Javascript or jQuery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.
function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}
Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"
With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1
Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle

Categories

Resources