簡體   English   中英

Webassembly-是否可以從API獲取/獲取JSON數據並在JS中進行操作?

[英]Webassembly - Is it possible to GET/Fetch JSON data from an API and manipulate in JS?

我是Webassembly的新手,它試圖處理從Webassembly編譯模塊中的文件或URL中獲取的JSON數據,並以Javascript獲取該JSON,以便可以在那里進行讀取和操作。

我發現的唯一示例來自https://kripken.github.io/emscripten-site/docs/api_reference/fetch.html ,它是從文件中提取並將其保存到瀏覽器IndexedDB的,但是結果是不可讀的字節,而不是JSON

fetch.c:

void downloadSucceeded(emscripten_fetch_t *fetch) {
printf("Finished downloading %llu bytes from URL %s.\n", fetch->numBytes, fetch->url);
// The data is now available at fetch->data[0] through fetch->data[fetch->numBytes-1];
emscripten_fetch_close(fetch); // Free data associated with the fetch.
}

void downloadFailed(emscripten_fetch_t *fetch) {
printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status);
emscripten_fetch_close(fetch); // Also free data on failure.
}

int main() {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_PERSIST_FILE;
attr.onsuccess = downloadSucceeded;
attr.onerror = downloadFailed;
emscripten_fetch(&attr, "./json/bol_list1.json");
}

將其編譯為.js可以正常工作,控制台記錄它從JSON文件下載的16507個字節,並將其插入IndexedDB,但結果不可讀,因為它與JSON JSON文件不同: https : //pastebin.com/ WBJrYgPR

IndexDB結果: IndexedDB結果屏幕截圖

那是一個字節串。 屏幕快照中的字節為123 034 111 114 105 110 097 108 082 101 113 117 101 115 034 058"{"orinalReques":

假設該字符串是utf-8解碼,請執行以下操作:

var enc = new TextDecoder("utf-8");
enc.decode(array);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM