【问题标题】:How do I convert an object which stores JSON data into an array in Angular?如何将存储 JSON 数据的对象转换为 Angular 中的数组?
【发布时间】:2018-07-15 17:24:29
【问题描述】:

我目前想使用 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
        }
    ]
}

【问题讨论】:

  • 您能否举例说明您开始使用的数据以及转换后的数据?
  • 我希望将对象转换为数组,它应该是这样的:array_name [ ] = [ [val,val,val].....[ ] ];我从一个存储我想要存储在对象中的 json 数据的对象开始

标签: javascript json angular tensorflow.js


【解决方案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);

【讨论】:

  • 你可以使用Object.values()而不是映射键
  • 是的,但浏览器支持更有限。
  • OP 正在使用 TypeScript - 所以浏览器支持并不重要。是吗?
  • @WandMaker 非常好!我的错。我已经更新了答案以反映这一点。
  • 我希望将对象转换为数组,它应该是这样的:array_name [ ] = [ [val,val,val].....[ ] ];我从一个存储我想要存储在对象中的 json 数据的对象开始。
【解决方案2】:

我认为下面的函数应该根据您的需要格式化 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
    }
    ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 2017-05-03
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多