简体   繁体   English

如何使用DOM2方法在Javascript中格式化JSON?

[英]How to format JSON in Javascript using the DOM2 method?

I am trying to build a live commenting system for my website (blog page homework). 我正在尝试为我的网站(博客页面作业)构建实时评论系统。 The idea is to use ajax to store comments in a mysql database and return JSON text for a comment without refreshing. 想法是使用ajax将注释存储在mysql数据库中,并返回JSON文本以生成注释,而无需刷新。 I am 90% done with this I am just not sure how to format the JSON text to not look like crap. 我完成了90%的工作,我只是不确定如何格式化JSON文本,使其看起来不像废话。

The JSON returned for a comment is the following: 返回的JSON注释如下:

[{"c_name":"Test Commenter","c_date":"2014-08-14","comment":"This is a test comment"}] [{“ c_name”:“测试评论者”,“ c_date”:“ 2014-08-14”,“评论”:“这是测试评论”}]

On my comment page (postdetails.php) firstly I query the comments database table and return all the comments already stored for a particular post. 首先,在我的评论页面(postdetails.php)上,查询评论数据库表,并返回已为特定帖子存储的所有评论。 The user then can enter a comment form > submit it > and the JSON text returned from the ajax function will appear on the page automatically. 然后,用户可以输入评论表单>提交>,并且ajax函数返回的JSON文本将自动显示在页面上。

The initial comments already stored in the table are displayed via a php function: 表中已经存储的初始注释通过php函数显示:

 // Display comments in nice format while ($row2 = mysql_fetch_array($result2)) { echo "<div class = 'comments_user'>\\n"; echo "<p>" . $row2['c_name'] ."</p>\\n"; echo "</div>\\n"; echo "<p class = 'date'>" . $row2['c_date'] . "</p>"; echo "<p>" . $row2['comment'] ."</p>\\n"; } 

My issue is getting the JSON text to display in a similar format underneath the previous comments. 我的问题是让JSON文本以类似的格式显示在先前的注释下方。 I have plain old HTML to display the JSON returned comments. 我有普通的旧HTML来显示JSON返回的注释。 Which I should be able to manipulate with the DOM by targeting the p tag id's shown below : 通过定位如下所示的p标签ID,我应该能够使用DOM进行操作:

    <!--Use JS and the DOM to manipulate this (live commenting)-->
        <div class = "comments_user">
            <p id = "commenter"></p>
        </div>
        <div class = "date_wrap">
            <p id = "c_date"></p>
        </div>
            <p id = "comment_content"></p>
        <hr/>
        </div>

The JSON text is returned in the 'result' variable. JSON文本在'result'变量中返回。 This is passed to my JSON_to_HTML function: 这被传递给我的JSON_to_HTML函数:

function JSON_to_HTML(result){
    var CM = JSON.parse(result);        

   // Need to traverse the DOM and display the array contents (JSON)
} 

My question is how can I add these comments using the DOM to the plain HTML code shown above? 我的问题是如何使用DOM将这些注释添加到上面显示的纯HTML代码中? I know i can get the p-tag values using document.getElementById("c_date"); 我知道我可以使用document.getElementById(“ c_date”);获得p标签值。 and so forth but how can I append children onto these? 依此类推,但是我该如何将孩子追加到这些孩子身上呢? I am familiar with the Jquery append method but for this I can ONLY use vanilla Javascript. 我熟悉Jquery append方法,但是为此,我只能使用香草Javascript。

One thing I tried was to have a global variable to track how many times the ajax function was called (number of comments per session). 我尝试做的一件事是使用一个全局变量来跟踪ajax函数被调用的次数(每个会话的注释数)。 For each comment I could traverse the DOM and append a child to the commenter, date and comment page. 对于每个评论,我都可以遍历DOM并将一个子代添加到评论者,日期和评论页面。 However I was not able to get this quite working. 但是,我无法使它正常工作。

My last resort would be to return the JSON into a simple table (which I know how to do). 我的最后一招是将JSON返回到一个简单的表中(我知道该怎么做)。

Any ideas here? 这里有什么想法吗? Thanks! 谢谢!

If you already have the html structure in place, you can do: 如果您已经有了html结构,则可以执行以下操作:

document.getElementById("c_date").textContent = CM[0].c_date;
// etc.

And if you want to add html instead of plain text, you can use innerHTML instead. 如果要添加html而不是纯文本,则可以使用innerHTML代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM