So parsen Sie einen JSON-String mit Jackson in ein Array

Lesezeit: 3 Minuten

So parsen Sie einen JSON String mit Jackson in ein Array
mmutilva

Ich habe ein String mit folgendem Wert:

[
  {
    "key1": "value11",
    "key2": "value12"
  },
  {
    "key1": "value21",
    "key2": "value22"
  }
]

Und die folgende Klasse:

public class SomeClass {
    private String key1;
    private String key2;
    /* ... getters and setters omitted ...*/
}

Und ich möchte es zu a analysieren List<SomeClass> oder ein SomeClass[]

Das ist der einfachste Weg, es zu tun Jackson ObjectMapper?

  • Ich hatte eine ähnliche Situation, entschied mich aber dafür, das JSONArray-Objekt zu deserialisieren, anstatt es mit einer Klasse zu tun, da dies in Zukunft Probleme mit der Klassenserialisierung vermeiden würde. Gibt es einen Vorteil gegenüber dem Suchen?

    – Arun

    13. August 12 um 13:54 Uhr


So parsen Sie einen JSON String mit Jackson in ein Array
mmutilva

Endlich habe ich es:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));

  • Was ist ObjectMapper? Können Sie bitte spezifizieren?

    – hitesh141

    5. Oktober 16 um 11:07 Uhr

  • @hitesh141 ObjectMapper ist ein Mitglied von com.fasterxml.jackson.databind.ObjectMapper

    – Chu

    21. November 16 um 08:21 Uhr

Die andere Antwort ist richtig, aber der Vollständigkeit halber hier noch andere Möglichkeiten:

List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });
SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);

Das vollständige Beispiel mit einem Array. Ersetzen “KonstruktArrayType()” durch “constructCollectionType()” oder jede andere Art, die Sie benötigen.

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class Sorting {

    private String property;

    private String direction;

    public Sorting() {

    }

    public Sorting(String property, String direction) {
        this.property = property;
        this.direction = direction;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public static void main(String[] args) throws JsonParseException, IOException {
        final String json = "[{"property":"title1", "direction":"ASC"}, {"property":"title2", "direction":"DESC"}]";
        ObjectMapper mapper = new ObjectMapper();
        Sorting[] sortings = mapper.readValue(json, TypeFactory.defaultInstance().constructArrayType(Sorting.class));
        System.out.println(sortings);
    }
}

Ich habe dieses Problem gelöst, indem ich den json auf JSONLint.com überprüft und dann Jackson verwendet habe. Unten ist der Code für dasselbe.

 Main Class:-

String jsonStr = "[{rn" + "       "name": "John",rn" + "        "city": "Berlin",rn"
                + "         "cars": [rn" + "            "FIAT",rn" + "          "Toyata"rn"
                + "     ],rn" + "     "job": "Teacher"rn" + "   },rn" + " {rn"
                + "     "name": "Mark",rn" + "        "city": "Oslo",rn" + "        "cars": [rn"
                + "         "VW",rn" + "            "Toyata"rn" + "     ],rn"
                + "     "job": "Doctor"rn" + "    }rn" + "]";

        ObjectMapper mapper = new ObjectMapper();

        MyPojo jsonObj[] = mapper.readValue(jsonStr, MyPojo[].class);

        for (MyPojo itr : jsonObj) {

            System.out.println("Val of getName is: " + itr.getName());
            System.out.println("Val of getCity is: " + itr.getCity());
            System.out.println("Val of getJob is: " + itr.getJob());
            System.out.println("Val of getCars is: " + itr.getCars() + "n");

        }

POJO:

public class MyPojo {

private List<String> cars = new ArrayList<String>();

private String name;

private String job;

private String city;

public List<String> getCars() {
    return cars;
}

public void setCars(List<String> cars) {
    this.cars = cars;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
} }

  RESULT:-
         Val of getName is: John
         Val of getCity is: Berlin
         Val of getJob is: Teacher
         Val of getCars is: [FIAT, Toyata]

          Val of getName is: Mark
          Val of getCity is: Oslo
          Val of getJob is: Doctor
          Val of getCars is: [VW, Toyata]

.

504300cookie-checkSo parsen Sie einen JSON-String mit Jackson in ein Array

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy