2015-11-23 109 views
1

我使用Three.js來保存和加載數據庫中的對象。我將我的對象插入爲JSON。JSON圖像url而不是base64

問題是,JSON.stringify或toJSON()在base64中轉換圖像url(紋理),並且我想保留http url。

這是originial 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": { 
     [...] 
    } 
} 

是否有解決方案做到這一點?

+0

你如何使用'stringify'函數? – Bfcm

+0

'var objectJson = SELECTED.toJSON(); \t \t \t objectJson = JSON.stringify(objectJson,null,'\ t'); objectJson = objectJson.replace(/[\n\t]+([\d\.e\-\[\]]+)/g,'$ 1');'' – sRcBh

回答

0

研究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 

而現在它的伎倆! :)