![](/img/trans.png)
[英]angular 2 - how do I iterate through a JSON response that contains an object which contains an array of objects?
[英]How do I convert an object which stores JSON data into an array in Angular?
我目前想使用tensorflow.js創建一個機器學習模型,但遇到了問題。
我將JSON內容保存在本地對象中,但是要在機器學習模型中使用它,我需要將其轉換為數組。
我有一個名為guru
的對象,該對象具有我的json對象數組,但是要在tensorflow.js中使用它,我需要一個像這樣的數組:
weather [ [1,2,3],[3,2,1],...[] ];
這是我的角度組件代碼:
export class DisplayjasonComponent implements OnInit {
public guru: {}; //local object
constructor(private http: HttpClient) {
var obj;
this.getJSON().subscribe(data => obj = data, error => console.log(error));
}
linearModel: tf.Sequential;
predection: any;
ngOnInit() {
this.getJSON().subscribe(data => {
this.guru = data; //saving json into local object
});
this.trainModel();
}
private _url: string = "/assets/myjson.json";
public getJSON(): Observable<any> {
return this.http.get(this._url);
}
async trainModel() {
this.linearModel = tf.sequential();
this.linearModel.add(tf.layers.dense({ units: 5, inputShape: [3] }));
this.linearModel.add(tf.layers.dense({ units: 2 }));
this.linearModel.compile({ loss: 'meanSquaredError', optimizer: 'sgd' });
const xs = tf.tensor2d([
[1, 2, 3], //this is where I need to use the array
[3, 2, 1]
]);
const ys = tf.tensor2d([
[1, 0],
[0, 1]
]);
await this.linearModel.fit(xs, ys)
const p = this.linearModel.predict(tf.tensor2d([[1, 2, 3]])) as any;
this.predection = Array.from(p.dataSync());
console.log(this.predection);
}
}
這是我的JSON文件:
{
"weather": [
{
"temprature": 23,
"precipitation": 2,
"humidity": 57,
"weather": 0
},
{
"temprature": 20,
"precipitation": 100,
"humidity": 87,
"weather": 1
},
{
"temprature": 32,
"precipitation": 5,
"humidity": 70,
"weather": 0
},
{
"temprature": 18,
"precipitation": 87,
"humidity": 93,
"weather": 1
},
{
"temprature": 28,
"precipitation": 0,
"humidity": 37,
"weather": 0
},
{
"temprature": 13,
"precipitation": 94,
"humidity": 93,
"weather": 1
},
{
"temprature": 25,
"precipitation": 4,
"humidity": 43,
"weather": 0
},
{
"temprature": 20,
"precipitation": 68,
"humidity": 98,
"weather": 1
},
{
"temprature": 26,
"precipitation": 0,
"humidity": 9,
"weather": 0
},
{
"temprature": 13,
"precipitation": 100,
"humidity": 98,
"weather": 1
}
]
}
我不確定您希望輸出數據是什么樣子,但是可以嘗試一下。
您實際上可以單行執行此操作:
data.weather.map(Object.values);
.map
函數用於轉換數組,而Object.values
函數將獲取對象中每個字段的值。
var data = { "weather": [ { "temprature": 23, "precipitation": 2, "humidity": 57, "weather": 0 }, { "temprature": 20, "precipitation": 100, "humidity": 87, "weather": 1 }, { "temprature": 32, "precipitation": 5, "humidity": 70, "weather": 0 }, { "temprature": 18, "precipitation": 87, "humidity": 93, "weather": 1 }, { "temprature": 28, "precipitation": 0, "humidity": 37, "weather": 0 }, { "temprature": 13, "precipitation": 94, "humidity": 93, "weather": 1 }, { "temprature": 25, "precipitation": 4, "humidity": 43, "weather": 0 }, { "temprature": 20, "precipitation": 68, "humidity": 98, "weather": 1 }, { "temprature": 26, "precipitation": 0, "humidity": 9, "weather": 0 }, { "temprature": 13, "precipitation": 100, "humidity": 98, "weather": 1 } ] }; const result = data.weather.map(Object.values); console.log(result);
我認為以下功能應根據您的需要格式化json。
function formatJson(json) {
let weather = [];
json.weather.forEach((obj) => {
let tempArray = Object.values(obj);
weather.push(tempArray);
});
return weather;
}
與JSON如下
let json = {
"weather": [{
"temprature": 23,
"precipitation": 2,
"humidity": 57,
"weather": 0
},
{
"temprature": 20,
"precipitation": 100,
"humidity": 87,
"weather": 1
},
{
"temprature": 32,
"precipitation": 5,
"humidity": 70,
"weather": 0
},
{
"temprature": 18,
"precipitation": 87,
"humidity": 93,
"weather": 1
},
{
"temprature": 28,
"precipitation": 0,
"humidity": 37,
"weather": 0
},
{
"temprature": 13,
"precipitation": 94,
"humidity": 93,
"weather": 1
},
{
"temprature": 25,
"precipitation": 4,
"humidity": 43,
"weather": 0
},
{
"temprature": 20,
"precipitation": 68,
"humidity": 98,
"weather": 1
},
{
"temprature": 26,
"precipitation": 0,
"humidity": 9,
"weather": 0
},
{
"temprature": 13,
"precipitation": 100,
"humidity": 98,
"weather": 1
}
]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.