How to create Two Level JSON - json

How to create Two Level JSON, if i have to get data in multiple lists, like below:
Category's List > Item's List
For an example: Sony > LED TV, Laptop, Phones etc..
Earlier i have created single Level JSON,
For an example: LED TV, Laptop, Phones see below:
[
{
"ProductID":"1",
"ProductName":"LED TV"
},
{
"ProductID":"2",
"ProductName":"Laptop"
}
]
So here my question is how my JSON should look like ?

You can use any JSON "data type" as values. So here, you would create an object whose keys are the categories and the values are arrays of products:
{
"Sony": [{
"ProductID": "1",
"ProductName": "LED TV"
}, {
...
}],
"Panasonic": [...]
}
Instead of using an array of products, you could also use an object of object, keyed by produce ID. Optimize the structure for your use case, i.e. structure it in such a way that you can easily access the information you need.
See http://json.org/ for a complete syntax description.

yeah I agree with #FelixKling in one of my app i have used same kind of JSON:
{
"Mixed Platter" : [
{
"title" : "Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.25"
},
{
"title" : "Non Veggie",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "5.75"
}
],
"Soups" : [
{
"title" : "Mulagatawny Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.75"
},
{
"title" : "Daal Soup",
"description" : "Lorem ipsum dolor sit amet, conse adipiscing elit.",
"cost" : "3.25"
}
]
}

[
{
"author": "anonymous",
"background": "0xaaaaaa",
"ball": "0xff1111",
"mat": "0xffff00",
"bouncer": "0xff00ff",
"obstacle": "0x00ddff",
"data":
[
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 0, 0, 0],
[1, 0, 1, 1, 0],
[1, 1, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 1],
[1, 1, 1, 0, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
[1, 1, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]
]
}
]

Related

How to create chart using Springboot Json api

i have this service that give back this Json result :
{
"MANNOUBA": 1,
"Medinine": 4,
"Gabes": 5,
"Tunis": 22,
"Beja": 3,
"Kebili": 0,
"Sfax": 11,
"Laundry Making": 6,
"italia": 0,
"Sousse": 6,
"Desk": 1,
"Jendouba": 3,
"Mahdia": 6,
"Ben Arous": 19,
"Zaghouan": 4,
"Gafsa": 0,
"Kairouan": 6,
"Monastir": 18,
"metos": 1,
"Eleonetech": 2,
"Nabeul": 22,
"Mannouba": 9,
"BENAROUS": 8,
"Ariana": 21,
"Bizerte": 3
}
i want to put this data in a Barchart For my angular Project with the names in the X axis and the Numbers in the Y axis
With using Highcharts, you can preprocess your data to the: [name, y] format and use category axis type. Example:
for (let key in data) {
chartData.push([key, data[key]]);
}
Highcharts.chart('container', {
xAxis: {
type: 'category'
},
series: [{
type: 'column',
data: chartData
}]
});
Live demo: http://jsfiddle.net/BlackLabel/cjk3uboh/
Highcharts angular wrapper: https://www.npmjs.com/package/highcharts-angular

Highcharts showing points instead of lines

*Desired output**
Actual output
The points don't become a line.
Question
How do I modify my code to achieve the desired output?
This is my code:
function create() {
var series = [];
$.ajax({
type: "POST",
url: url,
async: false,
success: function (result) {
var channels = Object.keys(result.result.data);
var size = channels.length;
var name = result.result.labels[1];
for (var i = 0; i < size; i++) {
var data = result.result.data[i][1];
var time = result.result.data[i][0];
var test = [];
test.push({
x:time,
y:data
});
series.push({
"name": name,
"data": test
});
}
}
}, false);
return series;
}
Here is the data returned from url
{
"api": 1,
"id": "system.cpu",
"name": "system.cpu",
"view_update_every": 1,
"update_every": 1,
"first_entry": 1464153889,
"last_entry": 1464154575,
"before": 1464154575,
"after": 1464154556,
"dimension_names": ["iowait"],
"dimension_ids": ["iowait"],
"latest_values": [0],
"view_latest_values": [0],
"dimensions": 1,
"points": 20,
"format": "json",
"result": {
"labels": ["time", "iowait"],
"data":
[
[ 1464154556000, 0],
[ 1464154557000, 0],
[ 1464154558000, 0],
[ 1464154559000, 0],
[ 1464154560000, 0],
[ 1464154561000, 0],
[ 1464154562000, 0],
[ 1464154563000, 0],
[ 1464154564000, 0],
[ 1464154565000, 0],
[ 1464154566000, 0],
[ 1464154567000, 0],
[ 1464154568000, 0],
[ 1464154569000, 0],
[ 1464154570000, 0],
[ 1464154571000, 0],
[ 1464154572000, 0],
[ 1464154573000, 0],
[ 1464154574000, 0],
[ 1464154575000, 0]
]
},
"min": 0,
"max": 96.9697
}

as3 Changing dynamic text in a symbol

I have a symbol tile with multiple keyframes. One of the keyframes has a dynamic text box with the instance name pTwo.
I want to change the text in pTwo to an empty string and have tried:
pTwo.text = String(""); //Atempt One
MovieClip.pTwo.text = String(""); //Attempt two
Any help would be appreciated. Thanks in advance
EDIT: Im making a tile game and using an array to make the map - my array is as follows:
public var myMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 3, 2, 2, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 0, 2, 0, 2, 0, 0, 0, 0, 1],
[1, 0, 2, 2, 0, 3, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 0, 1],
[1, 0, 2, 0, 2, 0, 2, 0, 0, 1],
[1, 0, 2, 2, 2, 0, 2, 0, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
All the arrays are made from the symbol called 'tile' which consists of different key frames. So each number in the array references a different key frame. I have a character and when the character walks over the tile- I want the dynamic text to change to empty to imitate an empty tile- alternatively if theres a way to replace the tile with a [0] , that would also be good.
my code that generates the board:
var mapWidth = 10;
var mapHeight = 10;
var tileSide = 32;
var totalTiles = mapWidth * mapHeight;
var myMap: Array = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 3, 2, 2, 2, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 0, 2, 0, 2, 0, 0, 0, 0, 0],
[1, 0, 2, 2, 0, 3, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 2, 2, 0, 1],
[1, 0, 2, 0, 2, 0, 2, 0, 0, 1],
[1, 0, 2, 2, 2, 0, 2, 0, 2, 1],
[1, 0, 0, 0, 0, 0, 2, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];
for (var i: int = 0; i < mapHeight; i++) {
for (var u: int = 0; u < mapWidth; u++) {
var cell: MovieClip = new tile();
cell.gotoAndStop(myMap[i][u] + 1);
cell.x = tileSide * u;
cell.y = tileSide * i;
addChild(cell);
};
};
Sorry i was not clear from the start. New at actionscript so i apologise in advance if my terminology is unclear.
Is this "tile" symbol available in all frames? If the playhead is moving, then the symbols within the MovieClip may become dereferenced. Add the "pTwo" object to your Watch list in debug and determine if it object exists as the "tile" symbol plays.
You may need to create a single frame symbol with 2 layers. One with the animated symbol and the other with the text.
If the TextField only exists on a particular frame, and you're sure that it's the current frame, you can use this:
TextField(getChildByName("pTwo")).text = "";
If you're not always sure, just wrap it in a try block:
try {
TextField(getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
If pTwo lives inside of a Movieclip, the code might look something like this:
try {
TextField(YourMovieclipsName.getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
Hope this helps!
Edit based on new info:
for (var i: int = 0; i < mapHeight; i++) {
for (var u: int = 0; u < mapWidth; u++) {
var cell: MovieClip = new tile();
cell.gotoAndStop(myMap[i][u] + 1);
cell.x = tileSide * u;
cell.y = tileSide * i;
try {
TextField(cell.getChildByName("pTwo")).text = "";
} catch(error:Error) {
trace("text field doesn't exist on this frame");
}
addChild(cell);
};
};

Scikit-Learn Encoding Categorical Features - ValueError: setting an array element with a sequence

I am trying to use sklearn.preprocessing.OneHotEncoder to binarize my categorical variables before use in some regression methods such as OLS, the Lasso etc.
I have a nested list like so:
l = [[0, 0, 0], [0, 1, 1], [1, 2, 2], [0, 3, 2], [1, 4, 0], [0, 5, 2], [2, 2, 2], [0, 6, 2], [1, 7, 2], [0, 8, 3], [3, 4, 2], [0, 8, 4], [0, 9, 2], [1, 7, 1], [0, 10, 2], [0, 2, 5], [1, 11, 2], [1, 2, 3], [4, 12, 2], [1, 4, 2], [0, 13, 2], [0, 14, 2], [0, 15, 2], [0, 16, 0], [0, 17, 6], [5, 17, 2], [4, 17, 2], [0, 17, 3], [0, 2, 6], [0, 8, 6], [4, 2, 2], [4, 4, 2], [5, 15, 3], [0, 2, 3], [0, 7, 2], [1, 15, 2], [0, 17, 2], [0, 8, 2], [0, 2, 2], [4, 16, 2], [0, 1, 2], [5, 15, 2], [4, 8, 0], [0, 18, 3], [3, 11, 2], [6, 7, 2], [0, 8], [0, 19, 2], [1, 1, 2], [0, 7, 0], [0, 1, 0], [0, 4, 2], [0, 15, 3], [7, 8, 2], [1, 8, 0], [1, 16, 2], [0, 20, 2], [1, 8], [1, 8, 2], [0, 11, 1], [1, 21, 2], [4, 1, 2], [5, 1, 2], [2, 1, 2], [0, 22, 2], [8, 8, 2], [1, 8, 3], [1, 17, 2], [0, 8, 7], [0, 0, 2], [7, 7, 2], [2, 2, 8], [9, 8, 2], [5, 8, 2], [4, 8, 2], [0, 4, 3], [0, 23, 0], [0, 24, 2], [0, 2, 0], [3, 1, 2], [0, 25, 2], [0, 2, 9], [0, 11, 2], [1, 12, 2], [1, 26, 3], [0, 23, 2], [0, 27, 3], [3, 8, 2], [6, 8, 2], [6, 27, 2], [0, 16, 2], [0, 28, 2], [0, 29, 2], [0, 8, 0], [0, 8, 10], [0, 27, 2], [4, 7, 2], [0, 21, 2], [6, 11, 2], [0, 30, 2], [2, 8, 2], [0, 23, 3]]
from sklearn import preprocessing
enc = preprocessing.OneHotEncoder()
enc.fit(l)
However, I am running into the error:
ValueError: setting an array element with a sequence
Here is the most telling callback as far as I can see:
C:\Program Files\Anaconda\lib\site-packages\numpy\core\numeric.pyc in asarray(a, dtype, order)
458
459 """
--> 460 return array(a, dtype, copy=False, order=order)
461
462 def asanyarray(a, dtype=None, order=None):
To try and solve this problem I have attempted to convert my list to a matrix and array in numpy but have had no luck.
In addition, I have made sure that each value in each of the nested lists is an integer. I have also tried converting them to floats, again with no success.
Any help would be wonderful. Thanks.

nvd3 plotting multiple series on stacked area chart

I'm trying to create a stacked area chart using nvd3.
Currently I'm creating the json data to be graphed and testing it against nvd3 live code section on nvd3 website, at the moment I've got this:
[
{
"values": [[1382812284000, 0], [1382818677000, 1], [1382818677000, 2], [1382819934000, 3], [1382820167000, 4], [1382822993000, 5], [1382823894000, 6], [1382825059000, 7], [1382827775000, 8]],
"key": "first"
},
{
"values": [[1382812284000, 0], [1382819096000, 1], [1382822399000, 2], [1382825092000, 3], [1382826256000, 4], [1382826291000, 5], [1382827074000, 6], [1382827332000, 7], [1382827374000, 8], [1382827662000, 9], [1382829230000, 10], [1382829230000, 11], [1382829230000, 12]],
"key": "second"
},
{
"values": [[1382812284000, 0], [1382826522000, 1], [1382826522000, 2], [1382826522000, 3], [1382826522000, 4], [1382826522000, 5], [1382826522000, 6], [1382826522000, 7], [1382826522000, 8], [1382829299000, 9], [1382830207000, 10], [1382830207000, 11], [1382830207000, 12], [1382830207000, 13], [1382830207000, 14], [1382830207000, 15], [1382830207000, 16]],
"key": "third"
}
]
I can't get the chart to be displayed though the legend is showing all three labels.
Anyway removing (any) two of the series and leaving just one the remaining one gets displayed, so I suppose the problem is not in the data itself but probably something in json structure.
Anyone could help?
I solved the problem: it seems the series must have the same number of elements!