[英]how to get json object from js array
let awards = ["earth","sun","moon"];
let a = JSON.stringify(awards);
console.log(a);
Result: -["earth","sun","moon"] // no difference?
在我的數據庫文本字段中,我有:
{"earth","sun","moon"};
更新
從這個我得到php數組使用:
$arr = json_decode($data['planets'], true);
["earth","sun","moon"]
和{"earth","sun","moon"}
和有什么區別
以及如何從js數組中獲取{"earth","sun","moon"}
- ["earth","sun","moon"];
?
對象的格式為{鍵:值}。 例如,{“ Earth”:“ Planet”}是一個對象。 但是{“ Earth”,“ Planet”}不是對象。
[“ earth”,“ sun”,“ moon”]是一個字符串數組。 無法將字符串數組轉換為對象,因為該操作實際上沒有任何意義。
您確定在數據庫文本字段中沒有缺少冒號嗎?
["earth","sun","moon"]
和{"earth","sun","moon"}
之間的區別在於前者是一個數組,后者是一個集合。
var words = ["earth","sun","moon"];
var set = new Set(words) // {"earth","sun","moon"}
至於json_decode
,您需要指定是否要關聯數組而不是對象。 這可能導致Array更改為Set更改為json.decode($data['planets'], true)
的數組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.