{"id":624,"date":"2023-01-15T14:06:01","date_gmt":"2023-01-15T05:06:01","guid":{"rendered":"https:\/\/www.dogrow.net\/android\/?p=624"},"modified":"2023-03-02T16:05:17","modified_gmt":"2023-03-02T07:05:17","slug":"blog32","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/android\/blog32\/","title":{"rendered":"(32) Jackson\u3067 JSON\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">1. \u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>Android\u30d7\u30ed\u30b0\u30e9\u30e0\u4e2d\u3067 JSON\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u305f\u3044\u3002<\/p>\n<h1 class=\"my_h\">2. \u3084\u3063\u3066\u307f\u308b<\/h1>\n<h3 class=\"my_h\">(1) Gradle\u306b Jackson\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/h3>\n<p>\u6700\u65b0\u7248\u306f\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3067\u78ba\u8a8d\u3059\u308b\u3002<br \/>\n<a href=\"https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-core\" target=\"_blank\" rel=\"noopener\">https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-core<\/a><br \/>\n<a href=\"https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-databind\" target=\"_blank\" rel=\"noopener\">https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-databind<\/a><br \/>\n<a href=\"https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-annotations\" target=\"_blank\" rel=\"noopener\">https:\/\/mvnrepository.com\/artifact\/com.fasterxml.jackson.core\/jackson-annotations<\/a><\/p>\n<pre class=\"brush: xml; highlight: [8,9,10]; title: ; notranslate\" title=\"\">\r\ndependencies {\r\n    implementation 'androidx.appcompat:appcompat:1.5.1'\r\n    implementation 'com.google.android.material:material:1.7.0'\r\n    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'\r\n    testImplementation 'junit:junit:4.13.2'\r\n    androidTestImplementation 'androidx.test.ext:junit:1.1.5'\r\n    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'\r\n    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.14.1'\r\n    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.14.1'\r\n    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.14.1'\r\n}\r\n<\/pre>\n<p>\u8a18\u8ff0\u5f8c\u306f\u3001Sync now \u30e1\u30cb\u30e5\u30fc\u3092\u9078\u629e\u3057\u3001\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<h3 class=\"my_h\">(2) JSON\u30c7\u30fc\u30bf\u3092\u4f5c\u308b\u3002<\/h3>\n<pre class=\"my_pre\">\r\n[\r\n  {\r\n    \"textA\": \"\u5c71\u7530\",\r\n    \"textB\": \"\u795e\u5948\u5ddd\",\r\n    \"textC\": \"\u81ea\u8ee2\u8eca\"\r\n  },\r\n  {\r\n    \"textA\": \"\u5009\u6ca2\",\r\n    \"textB\": \"\u4eac\u90fd\",\r\n    \"textC\": \"\u30d0\u30a4\u30af\"\r\n  }\r\n]\r\n<\/pre>\n<h3 class=\"my_h\">(3) JSON\u30c7\u30b3\u30fc\u30c9\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u304f\u3002<\/h3>\n<p>\u30c7\u30b3\u30fc\u30c9\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u683c\u7d0d\u3059\u308b\u30af\u30e9\u30b9\u3092\u4f5c\u308b\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Item {\r\n    private String  textA;\r\n    private String  textB;\r\n    private String  textC;\r\n\r\n    public String GetTextA(){return textA;}\r\n    public String GetTextB(){return textB;}\r\n    public String GetTextC(){return textC;}\r\n}\r\n<\/pre>\n<p>\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u51e6\u7406\u3092\u66f8\u304f\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport com.fasterxml.jackson.annotation.JsonAutoDetect;\r\nimport com.fasterxml.jackson.annotation.PropertyAccessor;\r\nimport com.fasterxml.jackson.core.JsonParseException;\r\nimport com.fasterxml.jackson.core.JsonProcessingException;\r\nimport com.fasterxml.jackson.databind.JsonMappingException;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\nvoid DecodeJson(String jsonText) {\r\n    \/\/ JSON\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002\r\n    ObjectMapper jsonMapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)\r\n                                                .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);\r\n    try {\r\n        Item&#x5B;] items = jsonMapper.readValue(jsonText, Item&#x5B;].class);\r\n        \/\/ \u30c7\u30fc\u30b3\u30fc\u30c9\u3057\u305f\u5185\u5bb9\u3092\u8868\u793a\u3057\u3066\u307f\u308b\u3002\r\n        for(Item i : items) {\r\n            String msg = String.format(&quot;A: %s, B: %s, C: %s&quot;, i.GetTextA(), i.GetTextB(), i.GetTextC());\r\n        }\r\n    } catch (JsonParseException e1) {\r\n        e1.printStackTrace();\r\n    } catch (JsonMappingException e2) {\r\n        e2.printStackTrace();\r\n    } catch (JsonProcessingException e3) {\r\n        e3.printStackTrace();\r\n    }\r\n}\r\n<\/pre>\n<h1 class=\"my_h\">3. \u5fdc\u7528<\/h1>\n<h3 class=\"my_h\">(1) \u5c11\u3057\u6c4e\u7528\u3063\u307d\u3044\u51e6\u7406\u306e\u5099\u5fd8\u9332<\/h3>\n<p>ObjectMapper.readTree()\u3092\u4f7f\u3044 JSON\u30c7\u30fc\u30bf\u304b\u3089 JsonNode\u578b\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3067\u69cb\u6210\u3055\u308c\u308b\u6728\u69cb\u9020\u3092\u751f\u6210\u3059\u308b\u3002<br \/>\n\u3000\u2192 \u30e9\u30d9\u30eb\u540d\u3092\u6307\u5b9a\u3057\u3066\u4efb\u610f\u306e\u30ce\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n\u3000\u3000\u2192 \u305d\u306e\u30ce\u30fc\u30c9\u306eJSON\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3002<br \/>\n\u3000\u3000\u3000\u2192 ObjectMapper.readValue()\u3067\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002<\/p>\n<p>\u4f8b\u3068\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a JSON\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u30b1\u30fc\u30b9\u306b\u3064\u3044\u3066&#8230;<\/p>\n<pre class=\"my_pre\">\r\n{\r\n  \"result\": \"0\",\r\n  \"data\" : \r\n     [\r\n       {\"a\":\"11\", \"b\":\"23\", \"c\":\"35\"},\r\n       {\"a\":\"43\", \"b\":\"38\", \"c\":\"61\"},\r\n       {\"a\":\"15\", \"b\":\"72\", \"c\":\"29\"}\r\n     ]\r\n}\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport com.fasterxml.jackson.annotation.JsonAutoDetect;\r\nimport com.fasterxml.jackson.annotation.PropertyAccessor;\r\nimport com.fasterxml.jackson.core.JsonParseException;\r\nimport com.fasterxml.jackson.core.JsonProcessingException;\r\nimport com.fasterxml.jackson.databind.JsonMappingException;\r\nimport com.fasterxml.jackson.databind.JsonNode;\r\nimport com.fasterxml.jackson.databind.ObjectMapper;\r\n\r\npublic class RestDecoder {\r\n    private JsonNode node;\r\n    private int resultCode;\r\n    ObjectMapper jsonMapper;\r\n\r\n    \/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\r\n    public RestDecoder(){\r\n        this.resultCode = -1;       \/\/ \u975e0(\u2260\u6b63\u5e38)\u3067\u521d\u671f\u5316\r\n    }\r\n\r\n    \/\/ \u30b5\u30fc\u30d0\u30fc\u304b\u3089\u53d7\u4fe1\u3057\u305f\u30c7\u30fc\u30bf(JSON\u5f62\u5f0f)\u3092\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002\r\n    public boolean execDecode(String jsonRawData) throws JsonParseException, JsonMappingException, JsonProcessingException {\r\n        this.jsonMapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)\r\n                                            .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);\r\n        this.node = this.jsonMapper.readTree(jsonRawData);\r\n        this.resultCode = this.node.get(&quot;result&quot;).asInt();\r\n        return this.isRestResultDone();\r\n    }\r\n\r\n    \/\/ \u30b5\u30fc\u30d0\u30fc\u304b\u3089\u53d7\u4fe1\u3057\u305f\u30c7\u30fc\u30bf\u4e2d\u306e\u300c\u5b9f\u884c\u7d50\u679c\u30b3\u30fc\u30c9\u300d\u3092\u53d6\u5f97\u3059\u308b\u3002\r\n    public boolean isRestResultDone() {\r\n        return 0 == this.resultCode;\r\n    }\r\n    public boolean isRestResultLessInventory() {\r\n        return RestResultCode.REST_RESULT_LESS_INVENBTORY == this.resultCode;\r\n    }\r\n    public boolean isRestResultLostItem() {\r\n        return RestResultCode.REST_RESULT_LOST_ITEM == this.resultCode;\r\n    }\r\n\r\n    \/\/ \u30b5\u30fc\u30d0\u30fc\u304b\u3089\u53d7\u4fe1\u3057\u305f\u5546\u54c1\u60c5\u5831(MyData\u578b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u914d\u5217)\u3092\u53d6\u5f97\u3059\u308b\u3002\r\n    public MyData&#x5B;] getDataAsItemData() throws JsonProcessingException {\r\n        MyData&#x5B;] myData = null;\r\n        if(this.isRestResultDone()) {       \/\/ \u6b63\u5e38\u306b\u30c7\u30fc\u30bf\u3092\u53d7\u4fe1\u6e08\u307f\uff1f\r\n            String jsonMyData = this.node.get(&quot;data&quot;).toString();\r\n            myData = this.jsonMapper.readValue(jsonMyData, MyData&#x5B;].class);\r\n        }\r\n        return myData;\r\n    }\r\n\r\n    \/\/ \u30b5\u30fc\u30d0\u30fc\u304b\u3089\u53d7\u4fe1\u3057\u305f\u30c7\u30fc\u30bf\u4e2d\u306e\u300c\u30a8\u30e9\u30fc\u30e1\u30c3\u30bb\u30fc\u30b8\u300d\u3092\u53d6\u5f97\u3059\u308b\u3002\r\n    public String getRestMessage() {\r\n        String msg = &quot;&quot;;\r\n        if(!this.isRestResultDone()){\r\n            msg = this.node.get(&quot;message&quot;).toString();\r\n        }\r\n        return msg;\r\n    }\r\n}\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>1. \u3084\u308a\u305f\u3044\u3053\u3068 Android\u30d7\u30ed\u30b0\u30e9\u30e0\u4e2d\u3067 JSON\u5f62\u5f0f\u306e\u30c7\u30fc\u30bf\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u305f\u3044\u3002 2. \u3084\u3063\u3066\u307f\u308b (1) Gradle\u306b Jackson\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u8ffd\u52a0\u3059\u308b\u3002 \u6700\u65b0\u7248\u306f\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3067\u78ba\u8a8d\u3059\u308b\u3002 https:\/\/\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/android\/blog32\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,15],"tags":[],"class_list":["post-624","post","type-post","status-publish","format-standard","hentry","category-androidstudio","category-java"],"views":784,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/624","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":17,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":703,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/posts\/624\/revisions\/703"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/android\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}