【问题标题】:how to parse individual JSON objects from one large JSON object如何从一个大型 JSON 对象中解析单个 JSON 对象
【发布时间】:2017-08-20 11:36:26
【问题描述】:

我正在尝试在一个大型 JSON 对象中解析多个 JSON 对象。现在它将整个 JSON 对象存储为一个对象,我需要将它们解析为单独的对象以存储在 mongodb 中。这是我的代码。

获取订单请求

WooCommerce.get('orders', function(err, data, res){
console.log(res);
var parsedOrders = JSON.parse(res);
var step;
for(step = 0; step < parsedOrders.length; step++){
    var emagispaceOrders = new Orders({orders: step});
    emagispaceOrders.save();
}    
});

用于在猫鼬模式中存储 JSON 对象的模型

var Schema = mongoose.Schema;



var orderSchema = new Schema({
orders: {type: Object}
});

var Orders = mongoose.model('Orders', orderSchema);

module.exports = Orders;

它保存订单对象,但不保存 JSON 对象中的数据。所以基本上总结一下,我的 mongodb 填充了专门的 8 个空对象,这些对象只包含下面显示的内容,而不是 JSON 对象发送的实际数据。

{ "_id" : ObjectId("58d93d05fdcd524224469604"), "orders" : 0, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469606"), "orders" : 2, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469605"), "orders" : 1, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469607"), "orders" : 3, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469608"), "orders" : 4, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd524224469609"), "orders" : 5, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960a"), "orders" : 6, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960b"), "orders" : 7, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960d"), "orders" : 9, "__v" : 0 }
{ "_id" : ObjectId("58d93d05fdcd52422446960c"), "orders" : 8, "__v" : 0 }

它应该包含通过 woocommerce 提交的完整订单,其中包含订单 ID、总计、客户名称等。

任何帮助将不胜感激。

编辑///////

猫鼬模式

var Schema = mongoose.Schema;



var orderSchema = new Schema({
id: {type: Number},
parent_id: {type: Number},
status: {type: String},
number: {type: Number},
customer_id: {type: Number},
shipping_total: {type: String},
total: {type: String},
total_tax: {type: String}
});

var Orders = mongoose.model('Orders', orderSchema);

module.exports = Orders;

订购 JSON 样本

{"id":6190,"parent_id":0,"status":"processing","order_key":"wc_order_58cc462aa2f9d","number":6190,"currency":"USD","version":"2.6.14","prices_include_tax":false,"date_created":"2017-03-17T20:25:00","date_modified":"2017-03-17T20:25:49","customer_id":1,"discount_total":"0.00","discount_tax":"0.00","shipping_total":"814.50","shipping_tax":"72.04","cart_tax":"361.54","total":"5335.59","total_tax":"433.58","billing":{"first_name":"John","last_name":"Doe","company":"Emagispace","address_1":"123 Billing Street","address_2":"Apt. #1","city":"Boulder","state":"CO","postcode":"80301","country":"US","email":"bt_buyer_us@paypal.com","phone":"312-123-4567"},"shipping":{"first_name":"John","last_name":"Doe","company":"Emagispace","address_1":"123 Billing Street","address_2":"Apt. #1","city":"Boulder","state":"CO","postcode":"80301","country":"US"},"payment_method":"cheque","payment_method_title":"Our team will send you an invoice for products ordered.","transaction_id":"","customer_ip_address":"75.166.79.141","customer_user_agent":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/56.0.2924.87 Safari\/537.36","created_via":"checkout","customer_note":"","date_completed":"2017-03-17T20:25:49","date_paid":"","cart_hash":"bf2aeb9eba51a119baeb896cb778093d","line_items":[{"id":207,"name":"3' Emagiblock","sku":"2000003A","product_id":5924,"variation_id":0,"quantity":22,"tax_class":"","price":"96.88","subtotal":"2131.25","subtotal_tax":"188.51","total":"2131.25","total_tax":"188.51","taxes":[{"id":1,"total":"188.5091","subtotal":"188.5091"}],"meta":[]},{"id":208,"name":"2' Emagiblock","sku":"2000002A","product_id":5920,"variation_id":0,"quantity":28,"tax_class":"","price":"52.49","subtotal":"1469.72","subtotal_tax":"130.00","total":"1469.72","total_tax":"130.00","taxes":[{"id":1,"total":"129.9967","subtotal":"129.9967"}],"meta":[]},{"id":209,"name":"1' Emagiblock","sku":"2000001A","product_id":5916,"variation_id":0,"quantity":10,"tax_class":"","price":"33.66","subtotal":"336.60","subtotal_tax":"29.77","total":"336.60","total_tax":"29.77","taxes":[{"id":1,"total":"29.7723","subtotal":"29.7723"}],"meta":[]},{"id":210,"name":"EmagiClip","sku":"4500001A","product_id":5984,"variation_id":0,"quantity":12,"tax_class":"","price":"12.50","subtotal":"149.94","subtotal_tax":"13.26","total":"149.94","total_tax":"13.26","taxes":[{"id":1,"total":"13.2622","subtotal":"13.2622"}],"meta":[]}],"tax_lines":[{"id":212,"rate_code":"US-CO-CO TAX-1","rate_id":"1","label":"CO Tax","compound":false,"tax_total":"361.54","shipping_tax_total":"72.04"}],"shipping_lines":[{"id":211,"method_title":"Weight Based Shipping","method_id":"main","total":"814.50","total_tax":"72.04","taxes":[{"id":1,"total":"72.042525"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[],"_links":{"self":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/orders\/6190"}],"collection":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/orders"}],"customer":[{"href":"https:\/\/www.emagispace.com\/wp-json\/wc\/v1\/customers\/1"}]}}

这是一个 JSON 响应对象。我收到的响应包含其中的 35 个,我需要从包含较小订单对象的主/大 JSON 对象中解析这些对象。

【问题讨论】:

    标签: javascript json node.js mongodb mongoose


    【解决方案1】:

    对象不是valid Mongoose schema types,这意味着您需要提前知道您的订单对象将采用什么形状,否则 Mongoose 将不会保存它们。

    编辑

    针对您的编辑,我刚刚注意到您没有尝试保存正确的对象。你在做

    Orders({orders: step});
    

    其中step 是一个索引,因此您实际上是在保存一个数字。你想做的是

    Orders({orders: parsedOrders[step]});
    

    这将引用正确的顺序。

    编辑 2

    实际上,您的架构在原始问题和编辑中有所不同。无论哪种方式,请记住,要获得订单,您需要通过parsedOrders[step] 引用它,其中step 是一个索引。

    【讨论】:

    • 即使我为猫鼬模式指定了每个参数,因为没有信息填充模型。
    • 请包含您的架构代码和示例订单对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2016-03-09
    相关资源
    最近更新 更多