【发布时间】:2014-06-27 15:32:16
【问题描述】:
我有一个用于 Wordpress 网站的 JSON api 插件,它会删除所有帖子。
在我的钛合金项目中,我正在获取 JSON 并对其进行解析,然后将其放入 tableview 中。
但是像 " 这样的特殊字符将被转换为 “ 并且 Titanium 会这样显示。如何在 Titanium 中解码?
JSON 获取代码:
var data = [];
var sendit = Ti.Network.createHTTPClient({
onerror : function(e) {
Ti.API.debug(e.error);
alert('There was an error during the connection');
},
timeout : 5000,
});
// Here you have to change it for your local ip
sendit.open('GET', 'http://development.webor.nl/driveeatsleep/api/get_posts/');
sendit.send();
// Function to be called upon a successful response
sendit.onload = function() {
var json = JSON.parse(this.responseText);
// var json = json.todo;
// if the database is empty show an alert
if (json.length == 0) {
$.hotelList.headerTitle = "The database row is empty";
}
// Emptying the data to refresh the view
// Insert the JSON data to the table view
var hotels = json.posts;
for ( var i = 0, iLen = hotels.length; i < iLen; i++) {
if(hotels[i].excerpt.length >= 50){
var excerpt = hotels[i].excerpt.substring(50,0) + '...' ;
}else{
var excerpt = hotels[i].excerpt;
}
// Remove HTML tags and coding
excerpt = excerpt.replace( /<[^>]+>/g,'');
data.push(Alloy.createController('row', {
icon : hotels[i].thumbnail,
title : hotels[i].title,
description : excerpt
}).getView());
// data.push(row);
Ti.API.info(hotels[i].thumbnail);
Ti.API.info(hotels[i].title);
}
$.hotelList.setData(data);
};
【问题讨论】:
-
检查响应头中是否有charset=utf-8'。否则,您可以尝试在 PHP 脚本中添加此标头:header('content-type: application/json; charset=utf-8');
-
是,标头:HTTP/1.1 200 OK 服务器:nginx 日期:2014 年 6 月 27 日星期五 15:42:13 GMT 内容类型:application/json; charset=utf-8 内容长度:184747 连接:关闭过期时间:星期四,1981 年 11 月 19 日 08:52:00 GMT 缓存控制:无存储,无缓存,必须重新验证,检查后 = 0,预check=0 Pragma: no-cache X-Pingback: development.webor.nl/driveeatsleep/xmlrpc.php Set-Cookie: _icl_current_language=nl; expires=2014 年 6 月 28 日星期六 15:42:12 GMT; path=/driveeatsleep/ X-Powered-By: PleskLin
-
好吧抱歉,我理解的有些不同。看这里:stackoverflow.com/questions/17678694/…
标签: php android ios json titanium