解析JSON时,在java中转义数字符号(哈希标记)

时间:2013-01-03 17:11:50

标签: java json parsing

我正在编写一个Android应用,我正试图从一个看起来像这样的JSON中获取#text字符串

{
"recenttracks": {
  "track": {
    "artist": {
      "#text": "Jeremy Fisher",
      "mbid": "7be30286-3510-4bc4-a587-51d9ad7c2b47"
 ...

但由于哈希标记,我得不到任何回报:

Error parsing data org.json.JSONException: No value for 

用mbid替换#text工作正常。代码:

try{
JSONObject container = new JSONObject(readLastFMFeed);
JSONObject recenttracks = container.getJSONObject("recenttracks");   
JSONObject track = recenttracks.getJSONObject("track");   
JSONObject artist = track.getJSONObject("artist");   
String sArtist = track.getString("#text");
Toast.makeText(LoggedIn.this, sArtist, Toast.LENGTH_LONG).show();
     }catch(JSONException e){
          Log.e("log_tag", " Error parsing data "+e.toString());
     }

如何转义哈希标记?

1 个答案:

答案 0 :(得分:3)

你不需要逃避它。你刚才犯了一个愚蠢的错误。而不是:

String sArtist = track.getString("#text");

写:

String sArtist = artist.getString("#text");