我需要幫助將JSON響應轉換爲UTF-8。 當我用UTF-8(沒有BOM)保存我的.json文件時,一切正常。當我只用UTF-8保存文件時,它不起作用,現在獲取JSON時應用程序崩潰。Android - 將JSON轉換爲UFT-8
在logcat中底部
來源:
..
public class JSONPARSER extends ListActivity {
private static String url = "http://profusum.se/neger.json";
private static final String TAG_CONTACTS = "messages";
private static final String TAG_NAME = "namn";
private static final String TAG_ID = "id";
private static final String TAG_KIK = "facebook";
private static final String TAG_IMGURL = "img";
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drivers);
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
contacts = json.getJSONArray(TAG_CONTACTS);
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME,}, new int[] {
R.id.inboxName, });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view.findViewById(R.id.inboxName)).getText().toString();
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
startActivity(in);
}
});
}
}
我搜索很難現在這個樣子,我想這是simple..but我不能弄明白。
05-01 21:13:08.213: E/JSON Parser(27153): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
摘要
當我保存與該文件 「UTF-8(無BOM)」 在記事本++解析是成功的。但是我在應用程序中看到這些奇怪的符號(請參閱示例)
但是,當我在Notepad ++中將文件保存爲「UTF-8」時,JSON爲我提供了正確的符號,但Android應用程序不會解析它。
Example
Tim Billström
哦,我忘了從「iso-8859-1」更改爲「utf-8」。感謝你的回答! – timbillstrom 2013-05-02 07:36:43
@TimLilleSkuttBillström:不客氣。將來請提及您使用的任何第三方庫/類(因爲Android API中沒有'JSONParser'類)。 – 2013-05-02 07:38:32
是的,我知道..昨天晚上去檢查JSONParser類。我的錯.. – timbillstrom 2013-05-02 07:40:20