2017-11-24 3 views
0

テキストファイルからjsonデータを抽出し、そのデータをHTMLテーブルに取り込みたいと思います。 .jspファイル。誰も私にサンプルコードを与えることはできますか?あなたは、AJAX機能に同じことをString myVar=jsonObject .toString();//which returns Stringのようないくつかの変数にJSONオブジェクトストアを取得することが使用するコードの上からjsonオブジェクトを含むテキストファイルを.jspファイルに読み込んでHTMLテーブルに入れます

<% public void loadJSON() { 
    JSONParser parser = new JSONParser(); 
    try { 
     Object obj = parser.parse(new FileReader("f:\\test.json")); 
     JSONObject jsonObject = (JSONObject) obj; 
     System.out.println(jsonObject); 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    }%> 

答えて

0

あなたはそれを読むためのコードを以下JSONParseのAPIをされて使用してJSONファイルを読み込むことができます各JSON要素を反復処理し、以下のような表を作成し、あまりにもこのスクリプトをインポートするには:詳細については<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<table id="records_table" border='1'> 
<tr> 
    <th>Rank</th> 
    <th>Content</th> 
    <th>UID</th> 
</tr> 
</table> 
<script> 
var response=<%=myVar%>//let see this is variable where JSON String is stored 
response = $.parseJSON(response); 

$(function() { 
$.each(response, function(i, item) { 
    $('<tr>').append(
     $('<td>').text(item.rank), 
     $('<td>').text(item.content), 
     $('<td>').text(item.UID) 
    ).appendTo('#records_table'); 
    // $('#records_table').append($tr); 
    //console.log($tr.wrap('<p>').html()); 
}); 
}); 
</script> 

を:Fill html table based on JSONと線でデバッグ行にしてみてください&幸運

関連する問題