Why parsing string into JSON (previously created with JSON.stringify()) works from console but from function it gives enigmatic error?
console.log(output); // in console i copy it into JSON.parse() and it works
output = JSON.parse(output); // Uncaught SyntaxError: Unexpected token index:1
My JSON:
{"type":"a","items":[{"id":"767758","id1":"1384882","id2":"1413749","c":"rgba(0, 100, 0, 5)","ls":"dashed","type":"l","d":{"t":"r","type":"1","hist":true},"w":5,"off":0},{"id":"6493942","id1":"1384882","id2":"5467332","c":"rgba(105, 105, 105, 5)","ls":"1","type":"l","d":{"t":"r","type":"h","hist":false},"w":5,"off":0},{"id":"1384882","id":"6c409d02-d937-11e4-a891-53b449010d08","d":{"t":"p","age":41,"xxx":5},"type":"n","t":"123","u":"p.png","g":[{"c":"rgb(255, 255, 255)","p":"ne","t":"5","fc":"rgb(0, 0, 0 )","w":false}],"x":-20.876105573962775,"y":41.26542299248838},{"id":"1413749","id":"e7e70a00-d3e4-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"zxc","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"42","fc":"rgb(0, 0, 0)","w":5},{"c":"rgb(0, 0, 255)","p":"nw","fc":"rgb(0, 0, 0)"}],"x":149.06285284387724,"y":5.308329729351229},{"id":"5467332","id":"8f0f5c30-d3d9-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"asd","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"","fc":"rgb(0, 0, 0)","w":false},{"p":"nw","fc":"rgb(0, 0, 0)"}],"x":-164.24347467678655,"y":-32.64876353378594}],"combos":{"iop":[],"dfg":[]},"jk":{"width":966,"height":890,"zoom":5,"offsetX":905,"offsetY":744}}
My code, there's something missing as others gave working JSfiddles:
var memory = '';
$buttonSave.click(function (event) {
if (helpers.isNUE(chart)) { return; }
var data = chart.serialize();
data = JSON.stringify(data).split(''); // string to array
data.forEach(function (datum) { // foreach character
memory += datum.charCodeAt(0).toString(2) + '2'; // get binary charcode, add padding "2"
});
console.info('memory saved: ' + memory);
event.preventDefault();
return false;
});
$buttonLoad.click(function (event) {
var data = memory.split('2'), // get binary code for each character
output = '',
serializedChart = {};
data.forEach(function (datum) {
output += String.fromCharCode(parseInt(datum, 2)); // read binary charcode and get character from it
});
console.warn('load done:');
try {
serializedChart = JSON.parse(output);
} catch (e) {
console.warn(e);
}
});
You are missing a final } from your json string. use this:
{
"type": "a",
"items": [
{
"id": "767758",
"id1": "1384882",
"id2": "1413749",
"c": "rgba(0, 100, 0, 5)",
"ls": "dashed",
"type": "l",
"d": {
"t": "r",
"type": "1",
"hist": true
},
"w": 5,
"off": 0
},
{
"id": "6493942",
"id1": "1384882",
"id2": "5467332",
"c": "rgba(105, 105, 105, 5)",
"ls": "1",
"type": "l",
"d": {
"t": "r",
"type": "h",
"hist": false
},
"w": 5,
"off": 0
},
{
"id": "6c409d02-d937-11e4-a891-53b449010d08",
"d": {
"t": "p",
"age": 41,
"xxx": 5
},
"type": "n",
"t": "123",
"u": "p.png",
"g": [
{
"c": "rgb(255, 255, 255)",
"p": "ne",
"t": "5",
"fc": "rgb(0, 0, 0 )",
"w": false
}
],
"x": -20.876105573962775,
"y": 41.26542299248838
},
{
"id": "e7e70a00-d3e4-11e4-b3ef-53b449010d08",
"d": {
"t": "c",
"active": true,
"r": 47
},
"type": "n",
"t": "zxc",
"u": "p.png",
"g": [
{
"c": "#ccff99",
"p": "ne",
"t": "42",
"fc": "rgb(0, 0, 0)",
"w": 5
},
{
"c": "rgb(0, 0, 255)",
"p": "nw",
"fc": "rgb(0, 0, 0)"
}
],
"x": 149.06285284387724,
"y": 5.308329729351229
},
{
"id": "8f0f5c30-d3d9-11e4-b3ef-53b449010d08",
"d": {
"t": "c",
"active": true,
"r": 47
},
"type": "n",
"t": "asd",
"u": "p.png",
"g": [
{
"c": "#ccff99",
"p": "ne",
"t": "",
"fc": "rgb(0, 0, 0)",
"w": false
},
{
"p": "nw",
"fc": "rgb(0, 0, 0)"
}
],
"x": -164.24347467678655,
"y": -32.64876353378594
}
],
"combos": {
"iop": [],
"dfg": []
},
"jk": {
"width": 966,
"height": 890,
"zoom": 5,
"offsetX": 905,
"offsetY": 744
}
}
Is output already parsed? If so, it looks like you're trying to parse output twice, if the output is already a JSON value, you do not need to parse it again.
You're missing an } at the end of your JSON. If you add that it should work.
{"type":"a","items":[{"id":"767758","id1":"1384882","id2":"1413749","c":"rgba(0, 100, 0, 5)","ls":"dashed","type":"l","d":{"t":"r","type":"1","hist":true},"w":5,"off":0},{"id":"6493942","id1":"1384882","id2":"5467332","c":"rgba(105, 105, 105, 5)","ls":"1","type":"l","d":{"t":"r","type":"h","hist":false},"w":5,"off":0},{"id":"1384882","id":"6c409d02-d937-11e4-a891-53b449010d08","d":{"t":"p","age":41,"xxx":5},"type":"n","t":"123","u":"p.png","g":[{"c":"rgb(255, 255, 255)","p":"ne","t":"5","fc":"rgb(0, 0, 0 )","w":false}],"x":-20.876105573962775,"y":41.26542299248838},{"id":"1413749","id":"e7e70a00-d3e4-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"zxc","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"42","fc":"rgb(0, 0, 0)","w":5},{"c":"rgb(0, 0, 255)","p":"nw","fc":"rgb(0, 0, 0)"}],"x":149.06285284387724,"y":5.308329729351229},{"id":"5467332","id":"8f0f5c30-d3d9-11e4-b3ef-53b449010d08","d":{"t":"c","active":true,"r":47},"type":"n","t":"asd","u":"p.png","g":[{"c":"#ccff99","p":"ne","t":"","fc":"rgb(0, 0, 0)","w":false},{"p":"nw","fc":"rgb(0, 0, 0)"}],"x":-164.24347467678655,"y":-32.64876353378594}],"combos":{"iop":[],"dfg":[]},"jk":{"width":966,"height":890,"zoom":5,"offsetX":905,"offsetY":744}}
What is typeof output? For parse() to work, it must be string but the dump in your question looks like it would return object. object means that the JSON has already been parsed and it's a JavaScript object.
So this would work:
var output = '{"type":"a","items":[{...}]}';
output = JSON.parse(output);
while this won't:
var output = {
"type":"a",
"items":[{...}]
};
output = JSON.parse(output);
I have a json with data. You can check it online on http://misterwolfs.be/keyboard/.
My json loads normally in safari but in chrome it won't get my data!
Dus anyone knows if there is something wrong with my json or if chrome is harder to get than safari?
This is how I try to get my json data :
$.get('resources/json/data.json',
function(json)
{
console.log('json', json);
keyboardController.keys = json.keyboard;
},
'json'
).success(function () { console.log('test success'); });
And this is my json file
{
"keyboard" : {
"esc": [{"Monday":80,"Tuesday":35,"Wednesday":63,"Thursday":51,"Friday":129,"Saturday":55,"Sunday":108,"Total":521}],
"f1": [{"Monday":6,"Tuesday":9,"Wednesday":18,"Thursday":4,"Friday":3,"Saturday":7,"Sunday":6,"Total":53}],
"f2": [{"Monday":9,"Tuesday":3,"Wednesday":5,"Thursday":18,"Friday":6,"Saturday":19,"Sunday":7,"Total":67}],
"f3": [{"Monday":2,"Tuesday":10,"Wednesday":9,"Thursday":3,"Friday":1,"Saturday":3,"Sunday":4,"Total":32}],
"f4": [{"Monday":9,"Tuesday":5,"Wednesday":3,"Thursday":7,"Friday":15,"Saturday":5,"Sunday":8,"Total":52}],
"f5": [{"Monday":3,"Tuesday":9,"Wednesday":6,"Thursday":4,"Friday":9,"Saturday":1,"Sunday":4,"Total":36}],
"f6": [{"Monday":5,"Tuesday":19,"Wednesday":9,"Thursday":3,"Friday":7,"Saturday":8,"Sunday":3,"Total":54}],
"f7": [{"Monday":8,"Tuesday":3,"Wednesday":4,"Thursday":9,"Friday":18,"Saturday":12,"Sunday":3,"Total":57}],
"f8": [{"Monday":3,"Tuesday":5,"Wednesday":7,"Thursday":14,"Friday":3,"Saturday":4,"Sunday":2,"Total":38}],
"f9": [{"Monday":8,"Tuesday":7,"Wednesday":14,"Thursday":4,"Friday":9,"Saturday":9,"Sunday":1,"Total":52}],
"f10": [{"Monday":3,"Tuesday":9,"Wednesday":4,"Thursday":6,"Friday":3,"Saturday":5,"Sunday":2,"Total":32}],
"f12": [{"Monday":6,"Tuesday":2,"Wednesday":2,"Thursday":9,"Friday":4,"Saturday":7,"Sunday":16,"Total":46}],
"f13": [{"Monday":9,"Tuesday":12,"Wednesday":6,"Thursday":17,"Friday":8,"Saturday":14,"Sunday":15,"Total":81}],
"#": [{"Monday":6,"Tuesday":10,"Wednesday":6,"Thursday":2,"Friday":12,"Saturday":7,"Sunday":30,"Total":73}],
"1": [{"Monday":204,"Tuesday":60,"Wednesday":89,"Thursday":52,"Friday":138,"Saturday":99,"Sunday":137,"Total":779}],
"2": [{"Monday":59,"Tuesday":22,"Wednesday":32,"Thursday":29,"Friday":89,"Saturday":92,"Sunday":97,"Total":419}],
"3": [{"Monday":44,"Tuesday":78,"Wednesday":60,"Thursday":35,"Friday":108,"Saturday":68,"Sunday":101,"Total":494}],
"4": [{"Monday":145,"Tuesday":156,"Wednesday":139,"Thursday":72,"Friday":40,"Saturday":58,"Sunday":117,"Total":727}],
"5": [{"Monday":248,"Tuesday":191,"Wednesday":188,"Thursday":90,"Friday":112,"Saturday":109,"Sunday":122,"Total":1060}],
"6": [{"Monday":28,"Tuesday":13,"Wednesday":31,"Thursday":15,"Friday":84,"Saturday":15,"Sunday":28,"Total":214}],
"7": [{"Monday":26,"Tuesday":29,"Wednesday":23,"Thursday":12,"Friday":59,"Saturday":17,"Sunday":22,"Total":188}],
"8": [{"Monday":31,"Tuesday":15,"Wednesday":26,"Thursday":28,"Friday":43,"Saturday":46,"Sunday":54,"Total":243}],
"9": [{"Monday":37,"Tuesday":23,"Wednesday":14,"Thursday":15,"Friday":30,"Saturday":50,"Sunday":138,"Total":165}],
"0": [{"Monday":94,"Tuesday":101,"Wednesday":99,"Thursday":87,"Friday":59,"Saturday":112,"Sunday":76,"Total":690}],
"°": [{"Monday":124,"Tuesday":105,"Wednesday":91,"Thursday":53,"Friday":32,"Saturday":40,"Sunday":88,"Total":521}],
"_": [{"Monday":105,"Tuesday":53,"Wednesday":60,"Thursday":91,"Friday":85,"Saturday":53,"Sunday":39,"Total":535}],
"del": [{"Monday":1474,"Tuesday":1639,"Wednesday":1311,"Thursday":875,"Friday":1911,"Saturday":997,"Sunday":1536,"Total":9743}],
"tab": [{"Monday":1189,"Tuesday":1773,"Wednesday":1379,"Thursday":834,"Friday":1632,"Saturday":989,"Sunday":1511,"Total":9307}],
"A": [{"Monday":880,"Tuesday":754,"Wednesday":520,"Thursday":290,"Friday":432,"Saturday":420,"Sunday":892,"Total":4188}],
"Z": [{"Monday":230,"Tuesday":271,"Wednesday":234,"Thursday":428,"Friday":829,"Saturday":204,"Sunday":236,"Total":2432}],
"E": [{"Monday":1370,"Tuesday":919,"Wednesday":670,"Thursday":411,"Friday":219,"Saturday":651,"Sunday":1144,"Total":5384}],
"R": [{"Monday":674,"Tuesday":790,"Wednesday":646,"Thursday":563,"Friday":438,"Saturday":687,"Sunday":1056,"Total":4854}],
"T": [{"Monday":728,"Tuesday":459,"Wednesday":323,"Thursday":206,"Friday":684,"Saturday":188,"Sunday":602,"Total":3190}],
"Y": [{"Monday":76,"Tuesday":201,"Wednesday":159,"Thursday":44,"Friday":58,"Saturday":48,"Sunday":142,"Total":728}],
"U": [{"Monday":236,"Tuesday":171,"Wednesday":114,"Thursday":76,"Friday":430,"Saturday":70,"Sunday":220,"Total":1317}],
"I": [{"Monday":561,"Tuesday":396,"Wednesday":257,"Thursday":196,"Friday":819,"Saturday":271,"Sunday":499,"Total":2999}],
"O": [{"Monday":763,"Tuesday":443,"Wednesday":353,"Thursday":221,"Friday":439,"Saturday":313,"Sunday":479,"Total":3011}],
"P": [{"Monday":305,"Tuesday":179,"Wednesday":122,"Thursday":106,"Friday":289,"Saturday":175,"Sunday":211,"Total":1387}],
"¨": [{"Monday":19,"Tuesday":8,"Wednesday":8,"Thursday":17,"Friday":34,"Saturday":3,"Sunday":7,"Total":96}],
"*": [{"Monday":20,"Tuesday":70,"Wednesday":30,"Thursday":17,"Friday":49,"Saturday":4,"Sunday":22,"Total":182}],
"caps": [{"Monday":49,"Tuesday":1,"Wednesday":39,"Thursday":2,"Friday":20,"Saturday":29,"Sunday":12,"Total":74}],
"Q": [{"Monday":55,"Tuesday":28,"Wednesday":654,"Thursday":24,"Friday":239,"Saturday":16,"Sunday":48,"Total":1064}],
"S": [{"Monday":717,"Tuesday":827,"Wednesday":411,"Thursday":487,"Friday":1012,"Saturday":506,"Sunday":864,"Total":4824}],
"D": [{"Monday":477,"Tuesday":499,"Wednesday":100,"Thursday":363,"Friday":232,"Saturday":260,"Sunday":507,"Total":2438}],
"F": [{"Monday":150,"Tuesday":150,"Wednesday":165,"Thursday":66,"Friday":101,"Saturday":54,"Sunday":212,"Total":898}],
"G": [{"Monday":270,"Tuesday":152,"Wednesday":115,"Thursday":89,"Friday":310,"Saturday":142,"Sunday":189,"Total":1267}],
"H": [{"Monday":359,"Tuesday":176,"Wednesday":50,"Thursday":105,"Friday":213,"Saturday":112,"Sunday":188,"Total":1203}],
"J": [{"Monday":131,"Tuesday":133,"Wednesday":195,"Thursday":48,"Friday":80,"Saturday":59,"Sunday":95,"Total":741}],
"K": [{"Monday":217,"Tuesday":215,"Wednesday":218,"Thursday":84,"Friday":128,"Saturday":137,"Sunday":201,"Total":1200}],
"L": [{"Monday":382,"Tuesday":261,"Wednesday":118,"Thursday":114,"Friday":248,"Saturday":140,"Sunday":341,"Total":1604}],
"M": [{"Monday":312,"Tuesday":158,"Wednesday":13,"Thursday":96,"Friday":213,"Saturday":151,"Sunday":249,"Total":1192}],
"%": [{"Monday":6,"Tuesday":10,"Wednesday":27,"Thursday":3,"Friday":23,"Saturday":1,"Sunday":13,"Total":83}],
"£": [{"Monday":5,"Tuesday":1,"Wednesday":12,"Thursday":3,"Friday":8,"Saturday":1,"Sunday":4,"Total":22}],
"enter": [{"Monday":1200,"Tuesday":878,"Wednesday":988,"Thursday":419,"Friday":1532,"Saturday":480,"Sunday":1015,"Total":6512}],
">": [{"Monday":62,"Tuesday":210,"Wednesday":185,"Thursday":114,"Friday":109,"Saturday":143,"Sunday":50,"Total":873}],
"W": [{"Monday":324,"Tuesday":75,"Wednesday":68,"Thursday":73,"Friday":265,"Saturday":66,"Sunday":370,"Total":1241}],
"X": [{"Monday":109,"Tuesday":264,"Wednesday":292,"Thursday":142,"Friday":89,"Saturday":134,"Sunday":137,"Total":1167}],
"C": [{"Monday":342,"Tuesday":367,"Wednesday":343,"Thursday":137,"Friday":263,"Saturday":195,"Sunday":286,"Total":1933}],
"V": [{"Monday":295,"Tuesday":121,"Wednesday":86,"Thursday":52,"Friday":238,"Saturday":71,"Sunday":275,"Total":1138}],
"B": [{"Monday":225,"Tuesday":454,"Wednesday":284,"Thursday":211,"Friday":382,"Saturday":376,"Sunday":191,"Total":2123}],
"N": [{"Monday":690,"Tuesday":102,"Wednesday":79,"Thursday":27,"Friday":373,"Saturday":90,"Sunday":524,"Total":1885}],
"?": [{"Monday":146,"Tuesday":268,"Wednesday":222,"Thursday":131,"Friday":200,"Saturday":27,"Sunday":49,"Total":1043}],
".": [{"Monday":264,"Tuesday":164,"Wednesday":145,"Thursday":83,"Friday":219,"Saturday":115,"Sunday":220,"Total":1210}],
"/": [{"Monday":173,"Tuesday":47,"Wednesday":79,"Thursday":79,"Friday":120,"Saturday":22,"Sunday":182,"Total":692}],
"+": [{"Monday":90,"Tuesday":64,"Wednesday":51,"Thursday":53,"Friday":43,"Saturday":29,"Sunday":36,"Total":366}],
"shift": [{"Monday":1197,"Tuesday":741,"Wednesday":760,"Thursday":421,"Friday":998,"Saturday":630,"Sunday":939,"Total":5686}],
"fn": [{"Monday":89,"Tuesday":60,"Wednesday":49,"Thursday":86,"Friday":100,"Saturday":32,"Sunday":84,"Total":500}],
"ctrl": [{"Monday":37,"Tuesday":10,"Wednesday":7,"Thursday":24,"Friday":30,"Saturday":27,"Sunday":70,"Total":205}],
"space": [{"Monday":1556,"Tuesday":3988,"Wednesday":692,"Thursday":541,"Friday":1932,"Saturday":970,"Sunday":1112,"Total":10791}],
"cmd": [{"Monday":1122,"Tuesday":1724,"Wednesday":1254,"Thursday":826,"Friday":1349,"Saturday":767,"Sunday":1478,"Total":8523}],
"alt": [{"Monday":96,"Tuesday":150,"Wednesday":97,"Thursday":28,"Friday":128,"Saturday":39,"Sunday":55,"Total":593}],
"left": [{"Monday":332,"Tuesday":1026,"Wednesday":307,"Thursday":229,"Friday":754,"Saturday":227,"Sunday":480,"Total":3355}],
"down": [{"Monday":394,"Tuesday":1438,"Wednesday":442,"Thursday":314,"Friday":912,"Saturday":226,"Sunday":432,"Total":4258}],
"right": [{"Monday":326,"Tuesday":1580,"Wednesday":436,"Thursday":194,"Friday":832,"Saturday":298,"Sunday":454,"Total":4120}],
"up": [{"Monday":207,"Tuesday":734,"Wednesday":205,"Thursday":114,"Friday":349,"Saturday":130,"Sunday":165,"Total":1904}]
}
}