用javascript對一個json數組深度賦值樣本,javascriptjson

來源:互聯網
上載者:User

用javascript對一個json數組深度賦值樣本,javascriptjson

<!DOCTYPE HTML> <html> <head> <title>test1</title> </head> <body> <script> var arr={ "name":"zhansan", "age":23, "address":{ "city":"beijing", "gas":{ "gasSheet":"氣態流出物月報", "H_adjust":1 }, "time":{ "duration":31 } }, "units":{"title":"function"} } function show(obj){ var tmp={}; for(var i in obj){ if(obj[i]=="object"){ <span style="color:#ff0000;">tmp[i]=show(obj[i]);</span> } else{ <span style="color:#ff0000;">tmp[i]=obj[i]</span> } } return tmp; } var result=show(arr); console.log(result); </script> </body> </html>
</pre><p></p><p>利用遞迴思想,其中注意指派陳述式</p><p>可用於不同的環境</p><p></p><pre>

JSON資料轉換為JavaScript數組問題

你的data就是數組,用eval後,再迴圈處理就ok,範例程式碼:
var str = '[{\"UId\":1,\"Uname\":\"a\",\"Pwd\":\"a1\"},{\"UId\":2,\"Uname\":\"b\",\"Pwd\":\"b1\"}]';eval("var json = " + str);// 轉換為json對象var data = []; for(var i=0,j=json.length;i<j;i++){ var item = []; item.push(json[i].UId); item.push(json[i].Uname); item.push(json[i].Pwd); data.push(item); }// 到這裡ok,這個data就是你要的資料了
 
怎用javascript定義對象數組,我要把對象數組轉成json傳到後台,

var students = [];
students[students.length] = new Object();
students[students.length] .name = "Lanny";
students[students.length] .age = "25";
students[students.length] .location = "China";
var json = JSON.stringify(students);

var student = new Object();
student.name = "Lanny";
student.age = "25";
student.location = "China";
students = (students || []).push(student);
var json = JSON.stringify(students);
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.