Frage zu gson lib, Beispiel [eventuell verschieben]

Hallo, zum Bleistift habe ich jetzt:

json-Format:

{ "search" : { "credit" : { "info" : "In order to use the free weather data from wetter.com you HAVE TO display at least two out of three of the following possibilities: text, link, logo",
          "link" : "http://www.wetter.com",
          "logo" : "Download at http://www.wetter.com/api/downloads/#logos",
          "text" : "Powered by wetter.com"
        },
      "exact_match" : true,
      "hits" : 1,
      "maxhits" : 30,
      "result" : [ { "adm_1_code" : "DE",
            "adm_2_name" : "Niedersachsen",
            "adm_4_name" : "Stade",
            "city_code" : "DE0001740",
            "name" : "Buxtehude",
            "plz" : "21614",
            "quarter" : ""
          } ],
      "search_string" : "Buxtehude"
    } }

(formatiert, aber wenig Overhead)

Daraus möchte ich jetzt zwei Dinge/Sachen machen,

  1. alle Name und Wert (Attribute oder so in json) einmal durchgehen und Zeichenkette erstellen,
  2. Java-Objekte erstellen,
  3. eine Liste mit Java-Objekten erstellen.

Wie muss ich da vorgehen (bei generell)?

Danke, Grüße, Cyborg. :rolleyes: :twisted:

*** Edit ***

zB hab ich jetzt:

        public static class Credit {
            public String info;
            public String link;
            public String logo;
            public String text;
        }
        public static class Result {
            public String adm_1_code;
            public String adm_2_name;
            public String adm_4_name;
            public String city_code;
            public String name;
            public int plz;
            public String quarter;
        }
        public Credit credit;
        public boolean exact_match;
        public int hits;
        public int maxhits;
        public List<Result> result;
        public String search_string;
    }```

```        String str = ...;
        System.out.println("str = " + str);
        Gson gson = new GsonBuilder().create();
        Search ser = gson.fromJson(str, Search.class);
        System.out.println("ser = " + ser);```

aber alles null bzw. 0 (auch keine Ausgabe). Was ist daran falsch?

Hat funktioniert, Thema gegessen/gelöst, denn anscheinend hat json bzw. dieser spezielle output ein Wurzel-“Elem.” Auch muss man die statisch inneren Klassen nicht schachteln(/kaskadieren), kann aber. So sieht’s nun aus:

        public Credit credit;
        public boolean exact_match;
        public int hits;
        public int maxhits;
        public List<Result> result;
        public String search_string;
        
        @Override
        public String toString() {
            return "Search{" + "credit=" + credit + ", exact_match=" + exact_match + ", hits=" + hits + ", maxhits=" + maxhits + ", result=" + result + ", search_string=" + search_string + '}';
        }
    }
    
    public static class Credit {
        public String info;
        public URL link;
        public String logo;
        public String text;
        
        @Override
        public String toString() {
            return "Credit{" + "info=" + info + ", link=" + link + ", logo=" + logo + ", text=" + text + '}';
        }
    }
    
    public static class Result implements Comparable<Result> {
        public String adm_1_code;
        public String adm_2_name;
        public String adm_4_name;
        public String city_code;
        public String name;
        public int plz;
        public String quarter;
        
        @Override
        public int compareTo(Result other) {
            if (this.city_code.length() < other.city_code.length()) {
                return 1;
            } else if (this.city_code.length() > other.city_code.length()) { // - else
                return -1;
            }
            return this.city_code.compareTo(other.city_code);
        }
        
        @Override
        public String toString() {
            return "Result{" + "adm_1_code=" + adm_1_code + ", adm_2_name=" + adm_2_name + ", adm_4_name=" + adm_4_name + ", city_code=" + city_code + ", name=" + name + ", plz=" + plz + ", quarter=" + quarter + '}';
        }
    }
    
    public static class Anfrage {
        public Search search;
        
        @Override
        public String toString() {
            return "Anfrage{" + "search=" + search + '}';
        }
    }
    
    public Anfrage getAnfrage(String str) throws JsonSyntaxException {
        Gson gson = new GsonBuilder().create();
        Anfrage anf = gson.fromJson(str, Anfrage.class);
        return anf;
    }```
```        String str = obj.getJson();
        System.out.println("str = " + str); // kontrolle
        Anfrage anf = obj.getAnfrage(str);
        Collections.sort(anf.search.result);
        System.out.println("anf = " + anf); // kontrolle```
Jetzt kann ich weiterverarbeiten. Eine gute Seite zum Aufbereiten von json ist: [JSON Format - your online JSON Formatter](http://jsonformat.com/#jsondataurllabel) .
Grüße.