Android JSON编码字符集问题
我的android应用程序Android JSON编码字符集问题,android,json,encoding,Android,Json,Encoding,我的android应用程序JSON编码有问题。无论我做什么,它都会将斜杠符号/输出为\/。我已经研究了一些有关这方面的问题,但还没有解决。 这是我的密码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aaa); if (android.os.Build.VERSION.SDK_INT &
JSON
编码有问题。无论我做什么,它都会将斜杠符号/
输出为\/
。我已经研究了一些有关这方面的问题,但还没有解决。这是我的密码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aaa);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform action on click
UUID uuid = UUID.randomUUID();
String randomUUIDString = uuid.toString();
Toast.makeText(MainTest.this, "button was pressed",
Toast.LENGTH_SHORT).show();
try {
JSONObject json = new JSONObject();
json.put("id", randomUUIDString);
json.put("photoUrl", "https://mysite.com/2011/10/image.jpg");
postData(json);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
public void postData(JSONObject json) throws JSONException {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(url_content);
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("json", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
Log.i("TEST", json.toString(1));
} catch (Exception e) {
e.printStackTrace();
}
}
这提示
{
"photoUrl": "https:\/\/mysite.com\/2011\/10\/image.jpg",
"id": "7b34aa16-3c80-40b3-b12b-6decdd38fabc"
}
你需要逃离这些伤痕
json.put(“photoUrl”,“https:\//mysite.com\/2011\/10\/image.jpg”)
还请注意,由于原始字符串中没有“\”,因此可以轻松地将“\”替换为“”
使用str.replace(“\”,”)
如果您使用的是Gson
,请尝试:
Gson gson = new GsonBuilder()..disableHtmlEscaping().create();
希望gson不会逃避您的html它是JSON选项。允许
\/
有助于在标记中嵌入JSON,这不允许您能以某种方式自动做到这一点吗?GSON对此有帮助吗?
console.log("AC\/DC"); // logs AC/DC
json.put("photoUrl", "https:\//mysite.com\/2011\/10\/image.jpg")