2016-02-03 79 views
0
var resData = {"request" : { "empid(Number)":"employeeid(Number)" , "msisdn(Number)":"empmsisdn(Number)" , "num(String)":"number(String)" , "add(String)":"address(String)" } , " response" : {"empid(Number)":"employeeid(Number)" , "msisdn(Number)":"empmsisdn(Number)" , "num(String)":"number(String)" , "add(String)":"address(String)" } } 

正常JSON鍵值HTML表被轉換成表,但使用此我想創建兩個表作爲關鍵值對以及它們的標題爲請求和使用JavaScript響應RESP。複雜JSON成使用javascript

回答

0

我還沒有創建UI,只是寫了邏輯打印'n'數量的複雜對象與控制檯中的表名檢查代碼片段。

var resData = {"request" : { "empid(Number)":"employeeid(Number)" , "msisdn(Number)":"empmsisdn(Number)" , "num(String)":"number(String)" , "add(String)":"address(String)" } , " response" : {"empid(Number)":"employeeid(Number)" , "msisdn(Number)":"empmsisdn(Number)" , "num(String)":"number(String)" , "add(String)":"address(String)" } }; 
 

 
var keys = Object.keys(resData); 
 
recursiveCall(keys,"tableVal",resData); 
 

 
function recursiveCall(keys,tableName,objectData){ 
 
\t for(var i=0;i<keys.length;i++){ 
 
     var values = objectData[keys[i]]; 
 
    if(typeof values==="object" || values instanceof Object){ 
 
     var newkey = Object.keys(values) \t   
 
     recursiveCall(newkey,keys[i],values); 
 
    }else{ 
 
    if(i==0){ 
 
     //create new Table 
 
     console.log("tableName: "+tableName); 
 
    } 
 
     \t console.log("new row: key:"+keys[i]+", value:"+objectData[keys[i]]); 
 
    } 
 
\t } 
 
}

+0

我相信這將幫助。 :) – zakaiter