2016-03-03 155 views
0

嗨,大家好,我在node.js中編寫了一些代碼,並且在Json格式的文本文件中有一些數據,我想把它們放在div中,但是我沒有任何輸出和代碼在這裏有錯誤的部分:「var curServer = serverData [server];」以及如何從數據獲取IP和端口,我不知道如何解決這個問題 有人能告訴我如何解決?!如何把格式化json格式的數據在div中

function populateServers(serverData) { 

    for (server in serverData.servers) { 

    var curServer = serverData[server]; // MY Problem IS here !!! 

    $('#serverBasket').append(
    '<a href="pmiw4m://' + ipv4FromNumber(curServer.address) + ':' + curServer.port + '"> \ 
     <div class="serverHolder"> \ 
       <div class="serverPrimary"> \ 
        <span class="hostname">'+ curServer.hostname + '</span><br /> \ 
        <span class="map">' + curServer.mapname + '</span><br /> \ 
        <span class="gametype">' + curServer.gametype + '</span><br /> \ 
        <span class="players">'+ curServer.clients + '/' + curServer.sv_maxclients + '</span> \ 
       </div> \ 
     </div> \ 
    </a>'); 
    } 
} 


$.getJSON("http://127.0.0.1/serevrlist/resopn2.txt", function (Response) { 
    $('#subtitle h3').text("DEDICATED SERVERS"); 
    populateServers(Response); 
}); 

resopn2.txt:

{ 
"servers":{ 
    "192.168.1.2:28961":{ 
    "hostname":"PlusMaster-Test", 
    "mod":"", 
    "gametype":"DOM", 
    "clients":"1", 
    "sv_maxclients":"18", 
    "mapname":"Crash", 
    "players":[ 
     { 
      "name":"MaximuM", 
      "ping":"999", 
      "score":"0" 
     } 
    ] 
    }, 
    "192.168.1.3:28962":{ 
    "hostname":"PlusMaster-Test2", 
    "mod":"", 
    "gametype":"DOM", 
    "clients":"1", 
    "maxclients":"18", 
    "mapname":"Crash", 
    "players":[ 
     { 
      "name":"MaximuM", 
      "ping":"999", 
      "score":"0" 
     } 
    ] 
    } 
}, 
"last_updated":"2016-03-03T10:07:12.929Z" 
} 
+0

你嘗試JSON.stringify和JSON.parse? var serverData = JSON.stringify(serverData); serverData = JSON.parse(serverData); 之前的行:for(服務器在serverData.servers){ – gehlotparesh

回答

1

你只需要做出一些改變。他們是如下:

resopn2.txt:

{ 
"servers":[ 
    { 
    "ip_address" : "192.168.1.2", 
    "port":"28961", 
    "hostname":"PlusMaster-Test", 
    "mod":"", 
    "gametype":"DOM", 
    "clients":"1", 
    "sv_maxclients":"18", 
    "mapname":"Crash", 
    "players":[ 
     { 
      "name":"MaximuM", 
      "ping":"999", 
      "score":"0" 
     } 
    ] 
    }, 
    { 
    "ip_address" : "192.168.1.3", 
    "port":"28962", 
    "hostname":"PlusMaster-Test2", 
    "mod":"", 
    "gametype":"DOM", 
    "clients":"1", 
    "maxclients":"18", 
    "mapname":"Crash", 
    "players":[ 
     { 
      "name":"MaximuM", 
      "ping":"999", 
      "score":"0" 
     } 
    ] 
    } 
], 
"last_updated":"2016-03-03T10:07:12.929Z" 
} 

變化功能:populateServers

function populateServers(serverData) { 
var serverData = JSON.stringify(test); 
serverData = JSON.parse(serverData); 
for (server=0; server < serverData.servers.length; server++) { 

var curServer = JSON.stringify(serverData.servers[server]); 
curServer = JSON.parse(curServer); 

$('#serverBasket').append(
'<a href="pmiw4m://' + ipv4FromNumber(curServer.address) + ':' + curServer.port + '"> \ 
    <div class="serverHolder"> \ 
      <div class="serverPrimary"> \ 
       <span class="hostname">'+ curServer.hostname + '</span><br /> \ 
       <span class="map">' + curServer.mapname + '</span><br /> \ 
       <span class="gametype">' + curServer.gametype + '</span><br /> \ 
       <span class="players">'+ curServer.clients + '/' + curServer.sv_maxclients + '</span> \ 
      </div> \ 
    </div> \ 
</a>'); 
} 
} 

希望它可以幫助