我有以下对象数组。目前,它有一个对象,其中包含多个对象。
let arr =
[
{
"data": {
"Score": {
"score": [
"87",
"21"
],
"Player": [
"Wiki",
"Tim"
]
},
"Designation": {
"By": [
"0",
"0",
"1",
"0",
"0"
],
"Position": [
"31/07/17",
"31/07/17",
"31/07/17",
"31/07/17",
"31/07/17"
]
},
"Address": {
"Location": "London",
"House_No": "43-B",
}
}
}
]上面的数据将放在一个表中。
我尝试过循环和插入,但没有找到任何方法。上面的数据不是常量,就像Position有5个元素一样,下一次可以是6,所以我不能简单地通过它的索引插入它。
我试过了,但没有成功。
发布于 2017-11-20 00:44:39
Mysql可以存储json数据,你可以在获取之后解析,甚至你也可以从mysql查询中解析json数据,但如果数据发生变化,这并不复杂,所以最好是存储、提取和解析。您可以选择feild类型JSON并将json存储在其中。
CREATE TABLE `book` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`tags` json DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;您可以使用php函数在其中插入json数据:
$json_data = json_encode($json_data);插入命令为:-
INSERT INTO `book` (`title`, `tags`)
VALUES (
'ECMAScript 2015: A SitePoint Anthology',
'$json_data'
);对于通过mysql查询操作json数据,有几个函数,比如JSON_ARRAY(),JSON_OBJECT()等等,你可以更喜欢使用它们。有关详细信息,请参阅以下文章:- https://www.sitepoint.com/use-json-data-fields-mysql-databases/
发布于 2017-11-20 01:19:56
在Mongodb中,如果你有动态数据,你可以使用混合类型模式,如下所示:
details:Schema.Types.Mixed,示例模式:-
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongoosePaginate = require('mongoose-paginate');
// create a schema
var casesSchema = new Schema({
sku: {type:String, unique:true},
details:Schema.Types.Mixed,
created_at: {type:Date, default: Date.now},
images: Schema.Types.Mixed,
ebay_hosted_images: Schema.Types.Mixed,
cloudinary_hosted_images: Schema.Types.Mixed,
dropbox_hosted_images: Schema.Types.Mixed,
is_listed:Boolean,
user_id : {type: String, ref:'User'}
});
casesSchema.plugin(mongoosePaginate);
var cases = mongoose.model('cases', casesSchema);
module.exports = cases;发布于 2017-11-20 00:54:34
在ES6中,您可以简单地获取对象的所有键和值,如下所示:
Object.keys(myObj).forEach(key => { console.log(key); // the name of the current key. console.log(myObj[key]); // the value of the current key. });
如果它是用于每次获取所有值的数组
arr.forEach(function(element) { console.log(element); });
arr.push(element)用于将元素推送到数组的最后一个索引。
https://stackoverflow.com/questions/47379196
复制相似问题