2011-11-01 22 views
1

我的應用程序涉及通過HTTP請求將JSON從RESTful Python後端服務器發送到Android設備。但是,我不斷收到JsonParseException。這裏是如何的JSON創建並返回:在Python中創建JSON並使用Java解析

articleList = [] 
obj = Content.objects.filter(id = 332).values()[0] 
articleList.append(obj)  
data = {'articleList' : articleList}   
return simplejson.dumps(data) 

的JSON看起來是這樣的:

{\"articleList\": 
    [ 
     {\"ArticleName\": \"Test_ArticleName\\n\", \"ArticleText\":\"Test_ArticleText\" 
     ,\"Author\": \"Test_Author\\n\", \"SourceID\": 18, \"Image\": \"NULL\", \"ImageLink\": \"NULL\", \"Video\": \"NULL\", \"PublicationDate\": \"8/15/2011 4:00AM\", \"VideoLink\": \"NULL\\n\", \"NumViews\": 0, \"Type\": \"NULL\", \"id\": 332} 
    ] 
} 

當我複製並粘貼此字符串轉換成Java,它的工作原理,所以我不認爲這個問題涉及結構。

在客戶端上我使用GSON庫解析JSON:

ArticleList articleList; 
InputStream source = retrieveStream(urlStr); 
Gson gson = new GsonBuilder().setVersion(1.0).create(); 
String json = convertStreamToString(source); 
try { 
    json = URLDecoder.decode(json,"UTF-8"); 
    articleList = gson.fromJson(json, ArticleList.class); 
} catch (UnsupportedEncodingException e1) { 
    Log.e(TAG,"Decoding error: "+e1); 
} catch (JsonParseException e) { 
    Log.e(TAG, "Parsing error: "+e); 
} 

private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
     } 
     Log.i(TAG, sb.toString()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

我ArticleList.class只包含

List<Article> articleList 

和我Article.class包含了所有的鑰匙 「ArticleName的」 ,「ArticleText」等。

我得到的例外是:

Parsing error: com.google.gson.JsonParseException: Expecting object found: "{\"articleList\": [{\"ArticleName\": \"Test_ArticleName\\n\", \"ArticleText\": \"Test_ArticleText\", \"Author\": \"Test_Author\\n\", \"SourceID\": 18, \"Image\": \"NULL\", \"ImageLink\": \"NULL\", \"Video\": \"NULL\", \"PublicationDate\": \"8/15/2011 4:00AM\", \"VideoLink\": \"NULL\\n\", \"NumViews\": 0, \"Type\": \"NULL\", \"id\": 332} 

我想我可能會因爲未轉義的引號而得到此異常,但我不知道該如何處理。我甚至在轉換爲ArticleList對象之前嘗試解碼JSON。

+0

嗯,嗯......是的,這是無效的JSON。查看所有反斜槓?不僅如此,而且你似乎將換行符和換行符連接在一起,然後期望它是有效的JSON,然後它不會。 –

+0

雙引號在Python中被轉義,但我不知道如何解除它們,因爲URL.decoder似乎沒有辦法。 – slouie

+0

您認爲「NULL」值實際上在Java中作爲Null處理嗎? – Lachezar

回答

2

將您提供的JSON字符串粘貼到Java程序的字符串中,可行 - 由於引號(充當字符串符號終止字符)會被轉義。這些引號應該hoever 逃脫,從外部資源(流,文檔或類似)讀取JSON的時候,因此,JSON應改爲:

{"articleList": 
    [ 
     {"ArticleName": "Test_ArticleName\n", "ArticleText":"Test_ArticleText" 
     ,"Author": "Test_Author\n", "SourceID": 18, "Image": "NULL", "ImageLink": "NULL", "Video": "NULL", "PublicationDate": "8/15/2011 4:00AM", "VideoLink": "NULL\n", "NumViews": 0, "Type": "NULL", "id": 332} 
    ] 
} 
+0

感謝您的回覆!我通過更改python代碼中的返回來解決問題,以返回HttpResponse(json.dumps(data),mimetype =「application/json」) – slouie