JSON

如何反序列化的JSON / GSON这可能是一个字符串,对象或列表

字号+ 作者:H5之家 来源:H5之家 2017-05-12 15:18 我要评论( )

如何反序列化的JSON / GSON这可能是一个字符串,对象或列表(how to deserialize a json/gson that could be a string , object ,or list) - IT屋-程序员软件开发

问 题

I have the following json

"notes": {"note": [ { "content": "Having wisdom teeth removed.", "from": "employee" }, { "content": "Get well soon", "from": "manager" } ]},

the issue is that the value coud also be

"notes": "",

or

"notes": {"note": { "content": "This is a test note.", "from": "employee" }},

and storing it in these

public class Notes { @SerializedName ("note") public List<Note> note; } public class Note { @SerializedName ("content") public String content; @SerializedName ("from") public String from; }

I believe I solved the issue of not being an array but being an single object by doing this

public class Json { private static Gson gson; private static class MyNoteClassTypeAdapter implements JsonDeserializer<List<RequestsDTO.Note>> { public List<RequestsDTO.Note> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) { List<RequestsDTO.Note> vals = new ArrayList<RequestsDTO.Note>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { vals.add((RequestsDTO.Note) ctx.deserialize(e, RequestsDTO.Note.class)); } } else if (json.isJsonObject()) { vals.add((RequestsDTO.Note) ctx.deserialize(json,RequestsDTO.Note.class)); } else { throw new RuntimeException("Unexpected JSON type: " + json.getClass()); } return vals; } } public static Gson getGson() { if (gson == null) { Type ListType = new TypeToken<List<RequestsDTO.Note>>() {}.getType(); GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(DateTime.class, new DateTimeSerializer()); builder.registerTypeAdapter(ListType, new MyNoteClassTypeAdapter()); gson = builder.create(); } return gson; } }

And now I am stuck on when the whole thing just comes back as a string....

解决方案

The idea is try to get "note" field (from "notes" JSONObject) as JSONArray first and if it throws exception that will mean that there is no "note" JSONArray into "notes" JSONObject and that will mean that "note" is JSONObject. The same way we can figure out situation when note field is String.

try { //String jsonString="{\"notes\": {\"note\": [{\"content\": \"Having wisdom teeth removed.\",\"from\": \"employee\" }, {\"content\": \"Get well soon\", \"from\": \"manager\"} ] }}"; //String jsonString="{\"notes\": { \"note\": {\"content\": \"This is a test note.\",\"from\": \"employee\"}}}"; String jsonString="{\"notes\": { \"note\": \"\"}}"; JSONObject jsonObject=new JSONObject(jsonString); JSONObject jsonObjectNotes=jsonObject.getJSONObject("notes"); try{ JSONArray jsonArrayNote=jsonObjectNotes.getJSONArray("note"); for (int i = 0; i < jsonArrayNote.length(); i++) { JSONObject jsonObject2= jsonArrayNote.getJSONObject(i); String stringContent=jsonObject2.getString( "content"); String stringFrom= jsonObject2.getString( "from"); Log.e(getClass().getName(), "content="+stringContent +"; from="+stringFrom); } } catch(JSONException e){ //that means that jsonObjectNotes has no jsonArray with name "notes" and "notes" is jsonObject try{ JSONObject jsonObject3=jsonObjectNotes.getJSONObject("note"); String stringContent=(String) jsonObject3.get( "content"); String stringFrom=(String) jsonObject3.get( "from"); Log.e(getClass().getName(), "content="+stringContent +"; from="+stringFrom); } catch(JSONException ex){ //that means that jsonObjectNotes has no jsonObject with name "notes" and "notes" is empty String String stringNote=jsonObjectNotes.getString("note") ; Log.e(getClass().getName(), "note is string ="+ stringNote); } } } catch (JSONException e) { e.printStackTrace(); }

In my example code another get operations can also throw jsonExceptions but I think you get the idea.

本文地址:IT屋 » how to deserialize a json/gson that could be a string , object ,or list

问 题

我有以下的JSON

“注意事项”:{“注意”: { “内容”:“如此智齿删除。” “从”:“员工” }, { “内容”:“快点好起来”, “从”:“经理人” } ]},

的问题是,值也coud是

“注意事项”:“”,

“注意事项”:{“注意”:{ “内容”:“这是一个测试注释。” “从”:“员工” }},

和将其存储在这些

公共课堂笔记 { @SerializedName(“注意”) 公开名单&LT;注意&GT;注意; } 公共类注意事项 { @SerializedName(“内容”) 公共字符串的内容; @SerializedName(“从”) 公共字符串; }

我相信我解决的不是一个数组但这样做是一个单一对象的问题

公共类的Json { 私有静态GSON GSON; 私有静态类MyNoteClassTypeAdapter实现JsonDeserializer&LT;列表&LT; RequestsDTO.Note&GT;&GT; { 公开名单&LT; RequestsDTO.Note&GT;反序列化(JsonElement JSON,类型typeOfT,JsonDeserializationContext CTX){ 名单&LT; RequestsDTO.Note&GT;瓦尔斯=新的ArrayList&LT; RequestsDTO.Note&GT;(); 如果(json.isJsonArray()){ 对于(JsonElement E:json.getAsJsonArray()){ vals.add((RequestsDTO.Note)ctx.deserialize(即RequestsDTO.Note.class)); } }否则如果(json.isJsonObject()){ vals.add((RequestsDTO.Note)ctx.deserialize(JSON,RequestsDTO.Note.class)); } 其他 { 抛出新的RuntimeException(“意外的JSON类型:”+ json.getClass()); } 返回丘壑; } } 公共静态GSON getGson() { 如果(GSON == NULL) { 键入ListType =新TypeToken&LT;列表&LT; RequestsDTO.Note&GT;&GT;(){} .getType(); GsonBuilder建设者=新GsonBuilder(); builder.registerTypeAdapter(DateTime.class,新DateTimeSerializer()); builder.registerTypeAdapter(ListType,新MyNoteClassTypeAdapter()); GSON = builder.create(); } 返回GSON; } }

而现在我坚持的时候,整个事情刚回来作为字符串上......

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 关于php处理JSON的有关问题

    关于php处理JSON的有关问题

    2017-05-12 15:17

  • json对日期的处置

    json对日期的处置

    2017-05-12 14:05

  • 对比分析json及XML【站长博客网】

    对比分析json及XML【站长博客网】

    2017-05-12 13:06

  • Python学习笔记之解析json的方法分析【站长博客网】

    Python学习笔记之解析json的方法分析【站长博客网】

    2017-05-10 17:07

网友点评