gpt4 book ai didi

json - 合并从wordpress导出的两个Json文件

转载 作者:行者123 更新时间:2023-12-04 13:12:50 30 4
gpt4 key购买 nike

我有两个从 wordpress 导出的具有相应 ID 的 Json 文件,我想将它们组合成一个 Json 文件,这样我就可以将它带入我正在使用 Gatsby JS 构建的网站中。其中一个文件是 posts.json另一个是 postsMeta.json . postsMeta 中的 post_id 对应 Posts 中的 ID

我将如何最好地合并两者?我可以运行某种 for在js中循环,我该怎么做?我在 Windows 上是否有某种 json explorer 可以帮助我做到这一点。

最后,我还想剪掉一些不必要的字段,例如 post_parent在帖子 json 和类似 meta_key 的内容中在postsMeta json中。

好的,希望这足够清楚,在此先感谢。

这是两个文件中第一个对象对应对的示例

帖子.json

{"ID":"19","post_author":"2","post_date":"2010-12-31 23:02:04","post_date_gmt":"2010-12-31 23:02:04","post_content":"Harry Potter was not available for the first sitting of the Halloween Picture. I hope everyone had a safe and fun Halloween. Tomorrow is picture retake day, please send back your previous prints if you want retakes. It is also hot lunch. See You tomorrow!","post_title":"Happy Halloween","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"happy-halloween","to_ping":"","pinged":"","post_modified":"2011-01-03 05:26:11","post_modified_gmt":"2011-01-03 05:26:11","post_content_filtered":"","post_parent":"0","guid":"http:\/\/localhost\/mrskitson.ca_wordpress\/?p=19","menu_order":"0","post_type":"post","post_mime_type":"","comment_count":"1"},

postMeta.json
{"meta_id":"27","post_id":"19","meta_key":"large_preview","meta_value":"http:\/\/www.mrskitson.ca\/wp-content\/uploads\/2010\/12\/halloween.jpg"},

更新:

this是尝试使用当前答案解决此问题,您可以在那里编辑代码。

最佳答案

How would I best go about merging the two?



您是否必须合并两个 JSON 文件/数据?

因为您可以从脚本中要求或加载 JSON 数据(甚至将它们放在 HTML 中),然后获取特定元字段/键的元值,这 function可以这样做:
// `single` has no effect if `meta_key` is empty.
function getPostMeta( post_id, meta_key, single ) {
let id = String( post_id ),
pm = [];
postsMeta.map( m => {
let a = ( ! meta_key ) ||
( meta_key === m.meta_key );

if ( a && id === m.post_id ) {
pm.push( m );
}
});

let meta = {},
mk = {};
pm.map( m => {
let k = m.meta_key, v;

if ( undefined === meta[ k ] ) {
meta[ k ] = m.meta_value;
} else {
v = meta[ k ];
if ( undefined === mk[ k ] ) {
meta[ k ] = [ v ];
mk[ k ] = 1;
}

meta[ k ].push( m.meta_value );
m[ k ]++;
}
});

pm = null;
mk = meta_key ? mk[ meta_key ] : null;

if ( mk ) {
return single ?
meta[ meta_key ][0] : // Returns a single meta value.
meta[ meta_key ]; // Returns all the meta values.
}

return meta_key ?
meta[ meta_key ] : // Returns the value of the `meta_key`.
meta; // Or returns all the post's meta data.
}

我用于测试的数据: (注意上面的 postsMeta/ getPostMeta()函数)
// Array of `post` objects.
const posts = [{"ID":"19","post_author":"2","post_date":"2010-12-31 23:02:04","post_date_gmt":"2010-12-31 23:02:04","post_content":"Harry Potter was not available for the first sitting of the Halloween Picture. I hope everyone had a safe and fun Halloween. Tomorrow is picture retake day, please send back your previous prints if you want retakes. It is also hot lunch. See You tomorrow!","post_title":"Happy Halloween","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"happy-halloween","to_ping":"","pinged":"","post_modified":"2011-01-03 05:26:11","post_modified_gmt":"2011-01-03 05:26:11","post_content_filtered":"","post_parent":"0","guid":"http:\/\/localhost\/mrskitson.ca_wordpress\/?p=19","menu_order":"0","post_type":"post","post_mime_type":"","comment_count":"1"}];

// Array of `meta` objects.
const postsMeta = [{"meta_id":"27","post_id":"19","meta_key":"large_preview","meta_value":"http:\/\/www.mrskitson.ca\/wp-content\/uploads\/2010\/12\/halloween.jpg"},{"meta_id":"28","post_id":"19","meta_key":"many_values","meta_value":"http:\/\/facebook.com"},{"meta_id":"29","post_id":"19","meta_key":"many_values","meta_value":"http:\/\/twitter.com"},{"meta_id":"30","post_id":"19","meta_key":"many_values","meta_value":"http:\/\/linkedin.com"}];

示例:(见 this Fiddle 演示)
// In these examples, we are retrieving the meta value for the post #19 (i.e. ID is 19).

// Retrieve a single value.
// Returns mixed; string, number, etc.
let url = getPostMeta( 19, 'large_preview', true );
console.log( url );

// Retrieve all meta values.
// Always returns an array of values.
let ms = getPostMeta( 19, 'many_values' );
console.log( ms, ms[0] );

// Retrieve all meta data.
// Always returns an object with meta_key => meta_value pairs. I.e. { key => value, ... }
let ma = getPostMeta( 19 );
console.log( ma, ma.large_preview, ma.many_values[0] );

但是如果你真的必须合并 JSON 数据,你可以这样做:(再次,参见 the same Fiddle 上的演示)
// Here we modify the original `posts` object.
posts.map( p => {
// Add all the post's meta data.
p.meta = getPostMeta( p.ID );

// Delete items you don't want..
delete p.post_parent;
delete p.menu_order;
// delete ...;
});

console.log( JSON.stringify( posts[0].meta ) ); // posts[0].meta = object
console.log( posts[0].post_parent, posts[0].menu_order ); // both are undefined

然后,如果您想复制粘贴新的/合并的 JSON 数据:
JSON.stringify( posts );

但是如果你真的只是想对帖子的元数据做点什么,你可以循环访问 posts反对并做事;例如。:
// Here the original `posts` object is not modified, and that we don't
// (though you can) repeatedly call `getPostMeta()` for the same post.
posts.map( p => {
// Get all the post's meta data.
let meta = getPostMeta( p.ID );

// Do something with `meta`.
console.log( meta.large_preview );
});

console.log( JSON.stringify( posts[0].meta ) ); // posts[0].meta = undefined
console.log( posts[0].post_parent, posts[0].menu_order ); // both still defined

// posts[0].meta wouldn't be undefined if of course posts[0] had a `meta` item,
// which was set in/via WordPress...

关于json - 合并从wordpress导出的两个Json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081588/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com