{"id":17183,"date":"2021-07-03T18:16:02","date_gmt":"2021-07-03T16:16:02","guid":{"rendered":"http:\/\/blog.wenzlaff.de\/?p=17183"},"modified":"2021-07-14T10:27:06","modified_gmt":"2021-07-14T08:27:06","slug":"java-reflexion-sortierung-fuer-org-json-jsonobject-hack-statt-fussball-em-goal","status":"publish","type":"post","link":"http:\/\/blog.wenzlaff.de\/?p=17183","title":{"rendered":"Java Reflexion Sortierung f\u00fcr org.json.JSONObject Hack, statt Fu\u00dfball EM &#8230;  Goal!"},"content":{"rendered":"<p>Werden mit <strong>org.json.JSONObject<\/strong> <a href=\"https:\/\/de.wikipedia.org\/wiki\/JavaScript_Object_Notation\" rel=\"noopener\" target=\"_blank\">Json Dateien<\/a> erstellt, werden die ldt. Spezifikation nicht sortiert. Das ist f\u00fcr die Auswertung mitunter <a href=\"http:\/\/blog.wenzlaff.de\/?p=17161#more-17161\" rel=\"noopener\" target=\"_blank\">schwer zu lesen<\/a>.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2021\/07\/json-ld-1536x1041-1.png\" alt=\"\" width=\"1536\" height=\"1041\" class=\"aligncenter size-full wp-image-17187\" srcset=\"http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2021\/07\/json-ld-1536x1041-1.png 1536w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2021\/07\/json-ld-1536x1041-1-300x203.png 300w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2021\/07\/json-ld-1536x1041-1-1024x694.png 1024w, http:\/\/blog.wenzlaff.de\/wp-content\/uploads\/2021\/07\/json-ld-1536x1041-1-768x521.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/> <center>JSON-LD Mindmap passt partiell zu JSON \ud83d\ude09<\/center><br \/>\nSchauen wir in die Klasse: <!--more--><\/p>\n<pre class=\"lang:java decode:true \" >    \r\n\r\n     public class JSONObject {\r\n\/\/ ...\r\n   \/**\r\n     * The map where the JSONObject's properties are kept.\r\n     *\/\r\n    private final Map&lt;String, Object&gt; map;\r\n\r\n\/\/ ...\r\n    \/**\r\n     * Construct an empty JSONObject.\r\n     *\/\r\n    public JSONObject() {\r\n        \/\/ HashMap is used on purpose to ensure that elements are unordered by \r\n        \/\/ the specification.\r\n        \/\/ JSON tends to be a portable transfer format to allows the container \r\n        \/\/ implementations to rearrange their items for a faster element \r\n        \/\/ retrieval based on associative access.\r\n        \/\/ Therefore, an implementation mustn't rely on the order of the item.\r\n        this.map = new HashMap&lt;String, Object&gt;();\r\n    }<\/pre>\n<p>Dann \u00fcberschreiben wir das mal mit:<\/p>\n<pre class=\"lang:java decode:true \" >\r\n\r\nprivate static JSONObject getSortJSONObject() {\r\n\r\n\t\tJSONObject json = new JSONObject();\r\n\t\t\/\/ magic zum sortieren der Parameter, so werden die Felder so\r\n\t\t\/\/ geschrieben wie hinzugef\u00fcgt also sortiert\r\n\t\ttry {\r\n\t\t\t\/\/ das Feld ist in der JSONObject Klasse so def.:\r\n\t\t\t\/\/ private final Map&lt;String, Object&gt; map;\r\n\t\t\t\/\/ wir machen ein neues Object des map Feldes\r\n\t\t\tField neueMap = json.getClass().getDeclaredField(\"map\");\r\n\r\n\t\t\t\/\/ noch beschreibbar machen, da private\r\n\t\t\tneueMap.setAccessible(true);\r\n\r\n\t\t\t\/\/ nun mit LinkedHashMap anstatt HashMap&lt;String, Object&gt;();\r\n\t\t\tneueMap.set(json, new LinkedHashMap&lt;&gt;());\r\n\r\n\t\t\t\/\/ Zugriff wieder unbeschreibbar, sicher ist sicher\r\n\t\t\tneueMap.setAccessible(false);\r\n\r\n\t\t} catch (IllegalAccessException | NoSuchFieldException e) {\r\n\t\t\tLOG.error(\"Error: \" + e.getLocalizedMessage());\r\n\t\t}\r\n\t\treturn json;\r\n\t}\r\n<\/pre>\n<p>Nun noch den vorhandenen Code:<br \/>\n<strong><br \/>\nJSONObject json = new JSONObject();<\/strong><\/p>\n<p>durch<\/p>\n<p><strong>JSONObject json = getSortJSONObject();<\/strong><\/p>\n<p>ersetzen. Cool <a href=\"https:\/\/de.wikipedia.org\/wiki\/Reflexion_(Programmierung)\" rel=\"noopener\" target=\"_blank\">reflection<\/a> &#8230;.  Goal!&#8230;  Goal!&#8230;  Goal!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Werden mit org.json.JSONObject Json Dateien erstellt, werden die ldt. Spezifikation nicht sortiert. Das ist f\u00fcr die Auswertung mitunter schwer zu lesen. JSON-LD Mindmap passt partiell zu JSON \ud83d\ude09 Schauen wir in die Klasse:<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[220,5,7],"tags":[2178,4959,1257,279,2669,2555,3360,4958],"class_list":["post-17183","post","type-post","status-publish","format-standard","hentry","category-anleitung","category-java","category-tools","tag-java","tag-java-reflection","tag-json","tag-map","tag-order","tag-reflection","tag-sortieren","tag-ueberschreiben"],"_links":{"self":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts\/17183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17183"}],"version-history":[{"count":0,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=\/wp\/v2\/posts\/17183\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17183"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.wenzlaff.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}