【问题标题】:JSON images url instead of base64JSON图像网址而不是base64
【发布时间】:2015-11-24 18:11:42
【问题描述】:

我正在使用 Three.js 在数据库中保存和加载对象。我将我的对象作为 JSON 插入。

问题是 JSON.stringify 或 toJSON() 在 base64 中转换图像 url(纹理),我想保留 http url。

这是原始 JSON:

{
    "metadata": {
        [...]
    },
    "geometries": [
        {
            [...]
        }],
    "materials": [
        {
            [...]
        }],
    "textures": [
        {
            [...]
        }],
    "images": [
        {
            "uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
            "url": "data:image/png;base64,iVBORw0KGgoATyuRZGZVREd0FAERRRBAEBEFwAYEyKqP/Rfozx+5fDYjvpeT/akv+n/J+/7eMjX9Ke8uojP4JVAYAyuj/Ld3Por7fPQ9i8d7vvr8LKNzLg/dn1u1hvQf3q/v92vRX0X/ [...]"
        }],
    "object": {
        [...]
    }
}

所需的 JSON 类型是这样的:

{
    "metadata": {
        [...]
    },
    "geometries": [
        {
            [...]
        }],
    "materials": [
        {
            [...]
        }],
    "textures": [
        {
            [...]
        }],
    "images": [
        {
            "uuid": "4ED9CE3E-7C8A-4EB7-8CF8-D90B3527DF5F",
            "url": "http://www.domain.com/picture/path.png"
        }],
    "object": {
        [...]
    }
}

有解决办法吗?

【问题讨论】:

  • stringify函数如何使用?
  • var objectJson = SELECTED.toJSON(); objectJson = JSON.stringify( objectJson, null, '\t' ); objectJson = objectJson.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );

标签: javascript json three.js base64


【解决方案1】:

在研究了 JSON 文件并获得了我的纹理路径后,我做了以下事情:

var objectParsed = SELECTED.toJSON(); //transform the selected object in JSON
textureBase64 = objectParsed.images[0].url; //get the value of the base64 encoded image
objectParsed.images[0].url = texMap; // replace the base64 encoded image with the http texture path

objectParsed = JSON.stringify( objectParsed, null, '\t' );
objectParsed = objectParsed.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' ); // make the JSON readable

现在它成功了! :)

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 1970-01-01
    • 2016-11-22
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2021-05-25
    • 2020-06-13
    相关资源
    最近更新 更多