清理后的代码中断

时间:2015-11-01 23:23:39

标签: javascript json coding-style code-cleanup

我是JavaScript的新手,我正在学习如何使我的代码更具可读性并远离内联函数。我有一个循环通过JSON注释数组的代码,然后将注释附加到DOM。当它“凌乱”它起作用,然后我突然试图清理它,由于某种原因它停止工作。

任何人都可以找出原因吗?这是一个“混乱”的:

public class EcoActivity extends Activity {
    String content1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
System.out.println(content1);
        setContentView(R.layout.activity_eco);
        WebView webview = (WebView)this.findViewById(R.id.WebView);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadDataWithBaseURL("", content1, "text/html", "UTF-8", "");
    }

private void runThread() {

    new Thread(new Runnable() {
        Document doc;
        public void run() {
            File input = new File("/tmp/input.html");
            try{
                doc = Jsoup.parse(input, "UTF-8", "http://www.muckendorf-wipfing.at/25-0-Wirtschaft+und+Gastronomie.html");
            }catch(Exception e){
                e.printStackTrace();
            }
            Element contentElement = doc.select("div#content").first();
            final String content2 = "<html>" + "<head></head>" + "<body>" + doc.getElementById("content").outerHtml() + "</body>" + "</html>";
            final String content = "<html>" + "<head></head>" + "<body>" + /**contentElement.toString() + */"</body>" + "</html>";
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            content1 = content2;
                            System.out.println("Plsrdme " + content2);
                        }
                    });
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }).start();
}
}

这是“干净的”:

var myCommentArray = [
    {
        _id: "888888888888888888",
        index: 1,
        name: "Perez",
        message: "First Comment .......",
        subject: "enim officias",
        replies: [ // notice this comment has replies (just 1 but it is still an array)
            {
                _id: "77777777777777777777",
                index: 0,
                name: "Reply to First Comment Ines for Perez",
                message: "...",
                subject: "reply subject consequat"
            }
        ]
    },
    {
        _id: "999999999999",
            index: 0,
            name: "Shelton",
            message: "2nd Comment....a",
            subject: "enim irure",
            replies: null // notice this comment has no replies and as such is null. this is better than an empty array
    },
    {
        _id: "666666666666666666",
        index: 2,
        name: "Perez",
        message: "3rd Comment.......",
        subject: "enim officias",
        replies: [
            {
                _id: "55555555555555555555",
                index: 0,
                name: "1st Reply to 3rd Comment",
                message: "...",
                subject: "reply subject consequat"
            },
            {
                _id: "44444444444444444444",
                index: 1,
                name: "2nd Reply to 3rd Comment",
                message: "...",
                subject: "reply subject consequat"
            }
        ]
    }
];

var stringedArray = JSON.stringify(myCommentArray);
var parsedCommentArray = JSON.parse(stringedArray);

$.each(parsedCommentArray, function (i, val) {
    var currentComment = parsedCommentArray[i];
    var commentsFormat = '<br> <div class="comment-avatar media-left"> <img src="http://placehold.it/50x50" alt="avatar">' +
        '</div><div class="comment-content media-body clearfix"> <div class="comment-avatar media-left"></div><h3 class="media-heading">' +
        currentComment.subject + '</h3> <div class="comment-meta">By ' + currentComment.name + '</div> <div class="comment-body"> <p>'
    + currentComment.message + '</p><a href="#" class="btn btn-gray more  reply">' +
        '<i class="fa fa-reply"> </i> Reply </a> </div> </div>';
    $('.comments').append(commentsFormat);
});

1 个答案:

答案 0 :(得分:0)

您的.getMyArray()方法已经返回了一个可以迭代的对象。不需要解析它,特别是var obj = JSON.parse(myCommentArray);,因为它不是有效的JSON字符串。你可以拥有

var myCommentArray = sabio.page.getMyArray();
$.each(myCommentArray, sabio.page.proccessComments);