我想从文件中导入 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>
但是我无法再访问它,因为它没有使用像“users”这样漂亮整洁的名称生成,正如您在下面看到的那样,它是“¬í 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();