javascript配列はjsonとjsonの値の捜索方法を回転します.
3622 ワード
直接コードを入れました.例があります.全部干物です.
<script>
/* */
var res = {
success: true, msg: "", datas: [{ wt_code: "G02", wt_name: " ", user_id: "001", user_name: " " },
{ wt_code: "G02", wt_name: " ", user_id: "002", user_name: " " },
{ wt_code: "G02", wt_name: " ", user_id: "003", user_name: " " },
{wt_code:"G03",wt_name:" ",user_id:"006",user_name:" "},
{wt_code:"G03",wt_name:" ",user_id:"008",user_name:" "}]
}
var datajson=undefined;
/* JSON*/
function arraytojson(arr1) {
if (arr1.length == 0) { return false; }
datajson={};
var key1 = "";
for (var i = 0; i < arr1.length; i++) {
key1=arr1[i]["wt_code"];
if (!datajson[key1]) {
datajson[key1] = { userList: [], name: arr1[i]["wt_name"] };
}
datajson[key1]["userList"].push({ user_id: arr1[i]["user_id"], user_name: arr1[i]["user_name"] });
}
}
/* JSON*/
function showJson() {
if (datajson==undefined){
arraytojson(res.datas);
}
document.getElementById("TextArea1").value = JSON.stringify(datajson);
}
/* */
function getallwt() {
if (datajson==undefined){
arraytojson(res.datas);
}
var tmpstr = "";
for (var key in datajson) {
tmpstr += key+"="+datajson[key]["name"] + ","
}
document.getElementById("TextArea1").value = tmpstr;
}
/* */
function getwtperson(wtcode) {
if (datajson == undefined) {
arraytojson(res.datas);
}
var tmpstr = "";
if (!datajson[wtcode]) {
document.getElementById("TextArea1").value = wtcode + " ";
return false;
}
if (datajson[wtcode]["userList"].length == 0) {
document.getElementById("TextArea1").value = datajson[name] + " ";
return false;
}
var userinfo = undefined;
for (var i = 0; i < datajson[wtcode]["userList"].length; i++) {
userinfo = datajson[wtcode]["userList"][i];
tmpstr += userinfo["user_id"] + "=" + userinfo["user_name"] + ",";
}
document.getElementById("TextArea1").value = tmpstr;
}
</script>
<input type="button" value=" json"/><br/>
<input type="button" value=" "/><br/>
<input type="button" value=" "/><br/>
<input type="button" value=" "/><br/>
<textarea id="TextArea1" cols="20" rows="20"/>
</code></pre>
<br/>
<br/>
</div>
</div>
</div>
</div>