天天看點

怎麼處理Java後端傳回坑爹的上劃線profile-image

TW的一個homework

其中有一個請求:http://thoughtworks-ios.herokuapp.com/user/jsmith

當時直接使用:HttpURLConnection請求的

傳回:

{/n  "profile-image": "http://img2.findthebest.com/sites/default/files/688/media/images/Mingle_159902_i0.png",/n  "avatar": "http://info.thoughtworks.com/rs/thoughtworks2/images/glyph_badge.png",/n  "nick": "John Smith",/n  "username": "jsmith"/n}/n

使用各種json、gson、fastjson 均無功而返,畢竟沒辦法建立一個屬性為:profile-image的類。

後來直接放棄,今天突然想起來,可以先把這個不标準的jsonstring使用字元串操作轉換為标準的jsonstring,然後再處理

如是:

result = result.replace("profile-image","profile_image");

result = result.replace("/n","");

之後:

Gson gson = new Gson();

UserBean res = gson.fromJson(result, UserBean.class);

OK , 大功告成。

收獲:對于Util的使用,一定要提供滿足Util的src,而不是想着怎麼使Util去适配你的src,畢竟src是你可以控制的,或者你自己去寫一個Util。

繼續閱讀