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 8 years ago.
Improve this question
I've been trying to found a simple way to format complex json files and turn it into readable html content.
The goal is to generates a set of nested boxes (html tables) containing the information of my JSON file.
Here is a sample of my json:
{"badgeGroups":
[{"name":"labAccess0", "badges" : ["AAAAAAAAAA"]},
{"name":"labAccess1", "badges" : ["1111111111","29006812B3"]}],
"inputs":
[{"name":"labDoorSecurityButton0", "state":false, "port":12}],
"outputs":
[{"name":"led0","equation":"labDoorSecurityButton0", "state":false, "port":0,"type":"GENERIC", "inputs": ...
Any ideas ?
The answer to your question depends entirely on your expected output formatting.
For example, bloopletech's json2html can parse arbitrary JSON input and a sample output can be seen over here.
If you want more customized formatting there's also json2html jQuery plugin.
All-in-all there are an infinite number of answers to your question as it's simply too broad.
function JSONtoXML(obj) {
var s = "";
var i=0;
s = s + "<" + obj[0];
var test = 0;
for(i=1;i<obj.length;i++) {
if(!(obj[i] instanceof Array) && !(typeof obj[i]==='string')) {
var obje = obj[i];
var k="";
for(prp in obje) {
k = k +" "+ prp + '="';
k = k+ obje[prp] + '" ';
}
s = s+k;
}
else if(!(typeof obj[i]==='string')){
if(test===0) {
s = s+">";
test++;
}
s = s + JSONtoXML(obj[i]);
}
else {
if(test===0) {
s = s+">";
test++;
}
s = s +obj[i];
}
}
if(test===0) {
s = s+">";
test++;
}
s = s+ "</" + obj[0] +">";
return s;
}
This will do the job to convert json to xml.
if you want better readability try some json beautifier.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
I'm asked to get attributes collection out of an array object,
let a = [
{name:'aname',age:21},
{name:'bname',age:22},
{name:'cname',age:23},
{name:'dname',age:24},
{name:'ename',age:25},
{name:'fname',age:26},
{name:'gname',age:27}]
// wanted
let ok = {
names:'aname;bname;cname;dname;ename;fname;gname',
ages:'21;22;23;24;25;26;27'
}
and I got 2 ways of doing it:
alpha just using map of an array:
// alpha
let res = {
names:'',
ages:''
}
res.names=a.map(iter=>iter.name).join(';')
res.ages=a.map(iter=>iter.age).join(';')
//then return res
// ========================================================
and beta just iterate the array and append each attribute in the tabulation array:
// beta
let res = {
names:[],
ages:[]
}
a.forEach(iter=>{
res.names.push(iter.name)
res.ages.push(iter.age)
})
// then handle res's fields
ok.names = res.names.join(';')
ok.ages = res.ages.join(';')
so which way should I use to get the collection? Will alpha get slower or faster than beta when the objects in a get lots of fields(attrs)?
Both approaches are good. I'd say it depends on your personal preference what you'd want to use.
However, It seems to me that if you are aiming for performance, the following would yield better results.
let a = [
{name:'aname',age:21},
{name:'bname',age:22},
{name:'cname',age:23},
{name:'dname',age:24},
{name:'ename',age:25},
{name:'fname',age:26},
{name:'gname',age:27}]
let ok = { names: '', ages: ''}
for (let i = 0; i < a.length; i++){
const iter = a[i]
ok.names += iter.name + ";";
ok.ages += iter.age + ";";
}
ok.names = ok.names.slice(0,-1)
ok.ages = ok.ages.slice(0,-1)
console.log(ok)
This apporach eliminates the need to create new arrays or joining them (join is a heavy operation). Just create the string you want and at the end of it all, remove the one extra semicolon.
I consider that alfa is simpler and clearer for me, but I guess it is up to you...
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 3 days ago.
Improve this question
Is this the right way to remove a given character from String?
////not using replace method
function removeChar(str1, s) {
let tempĀ = str1.split('')
let temp2 = []
for (i = 0; i < temp.length; i++) {
if (temp[i] != s) {
temp2.push(temp[i])
}
}
console.log(temp2.join(''));
}
removeChar("Hello","l")
You can do something like this
function removeChar(str1, s) {
return str1.split(s).join('')
}
this is another solution without replace()
const removeChar = (word, letter) => word.split("").filter(v => v !== letter).join("");
Just use the inbuilt replace function of javascript.
newString = oldString.replace(characterToBeReplaced, '');
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 3 years ago.
Improve this question
my code is not working at all
I need to solve this quiz
question is write convertToString as function !
this function should convert to string from parameter
ex )
let output = convertToString(120);
console.log(output); // --> '120'
let output2 = convertToString('hello');
console.log(output2); // --> 'hello'
let output3 = convertToString(true);
console.log(output3); // --> 'true'
this is what I wrote
function convertToString(anything) {
if (typeof anything === 'number' && typeof anything === 'boolean') {
let ret = anything.toString()
} else {
return anything;
}
return ret1;
}
convertToString(120);
The easiest way to convert anything is by making + operation with ""
function convertToString(anything) {
return "" + anything
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
console.log(convertToString(null));
console.log(convertToString(undefined));
Zero checks necessary.
function convertToString(val) {
return String(val);
// or return val.toString();
// or return '' + val;
}
console.log(convertToString(12));
console.log(convertToString(true));
console.log(convertToString('hello'));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a javascript with the unixtimestamp and the price of an item at that particular time. The timestamp is coming in string as listed below. How do I remove the double quotes from the timestamp. This is the array. I used the string replace function but not working.
["1356998400000", 222.69179362385]
["1357084800000", 209.18952317885]
["1357171200000", 211.95012017103]
["1357257600000", 200.15913266219]
["1357344000000", 215.58462758679]
var arr = [["1356998400000", 222.69179362385],
["1357084800000", 209.18952317885],
["1357171200000", 211.95012017103],
["1357257600000", 200.15913266219],
["1357344000000", 215.58462758679]];
arr.forEach(function(item){
item[0] = Number(item[0])
})
console.log(arr);
Just Use Number() to make a string containing number to number.
Something like this?
var myArr = ["1356998400000", 222.69179362385,
"1357084800000", 209.18952317885,
"1357171200000", 211.95012017103,
"1357257600000", 200.15913266219,
"1357344000000", 215.58462758679];
//check the values in the array before making changes
console.log(myArr);
var i;
for(i = 0; i < myArr.length; i++) {
if(typeof myArr[i] == "string") {
myArr[i] = parseFloat(myArr[i]);
}
}
//check the value of the array after changes
console.log(myArr);
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 5 years ago.
Improve this question
I have the following function on my .html page:
function Success(data)
{
var obj;
obj=JSON.parse(data);
document.write(obj);
}
that returns the objects as Hi,Hello,How,Are,You,Me,They,Them
I would like to achieve the following : Like this
How do I do this?
If obj is a string then you can split it's content by ',' and generate a table as follows:
var obj;
obj=JSON.parse(data);
obj = obj.split(',')
result = '<table><tr>';
obj.forEach(function (item, i) {
result += '<td>' + item +'</td>';
if ((i + 1) % 4 == 0) result += '</tr><tr>';
})
result += '</tr></table>';
document.write(result);
you will probably need to style the table after.