如何获取用于HTML的JSON文件的特定部分?

时间:2013-03-07 14:57:14

标签: java html json

我想从文件导入JSON数据,我现在的代码是:

<script>                            
    $.getJSON('data.json', function(data) {             
    var output = "<tr>";
    for ( var i in data.users) {
    output += "<td>"
    + "-- "
    + data.users[i].time
    + " --<br>-- "
    + data.users[i].senderID
    + " --<br>"
    + data.users[i].message
    + "<br></br></td>";
    }
    output += "</tr>";
    document.getElementById("placeholder").innerHTML = output;
    });
</script>

但是我无法再访问它,因为它不会像“用户”这样漂亮的名称生成,因为你可以在下面看到它是“¬ít”但你不能引用它作为它的一部分是无效的字符

    ¬í t’{  
    "messageId": 53,  
    "userTo": {    
    "userId": 4,    
    "userName": "Bob123",    
    "userLastCheckedDate": "Mar 7, 2013 11:14:53 AM"
    },

    "userFrom": {
    "userId": 1,
    "userName": "Joe123",    
    "userLastCheckedDate": "Mar 7, 2013 10:41:44 AM"
    },

    "messageContent": "a lovely message here",
    "messageSentDate": "Mar 7, 2013 11:36:14 AM",
    "messageReadDate": "Mar 7, 2013 12:49:52 PM"
    }

有什么想法吗?谢谢!

此外,这是生成JSON的java

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String json = gson.toJson(userMessages.get(0));
    out.write(json);

    FileOutputStream fout = new FileOutputStream("D:\\web\\data.txt");
    ObjectOutputStream oos = new ObjectOutputStream(fout);   
    oos.writeObject(json);
    oos.close();

0 个答案:

没有答案