T
T
thatmaniscool2022-01-09 17:27:14
Java
thatmaniscool, 2022-01-09 17:27:14

How to parse this JSON string?

There is a line like this:

[
  {
    "email": "[email protected]",
    "primary": true,
    "verified": true,
    "visibility": "private"
  }
]

That is, it is an array. I'm trying to parse it using the json.org library, but so far to no avail.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2022-01-09
@thatmaniscool

looked at the code - no errors found. the problem was with imported org.json.jso.jar -

V
Vasily Bannikov, 2022-01-09
@vabka

I'm trying to parse it using the json.org library

json.org is a site where Json is described and links to libraries for different languages ​​are collected.
For Java there is a whole pack:

Java
  • json-java
  • JSONUtil
  • jsonp
  • json-lib
  • Stringtree
  • SOJO
  • json-taglib
  • flexjson
  • Argo
  • jsonij
  • fast json
  • mjson
  • jjson
  • json-simple
  • json-io
  • google-gson
  • FOSS Nova JSON
  • Corn CONVERTER
  • Apache johnzon
  • Genson
  • cookjson
  • progbase
  • jackson
  • MOXy


Which one are you interested in?
As far as I know, the two most popular libs in Java are Jackson and gson, so I'll give an example for both of them:
First, we declare a class:
public class Something {
    public String email;
    public boolean primary;
    public boolean verified;
    public String visibility;
}

Jackson

// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
ObjectMapper om = new ObjectMapper();
Something[] root = om.readValue(jsonString, Something[].class);


Gson

//import com.google.gson.Gson;
Gson gson = new Gson();
Something[] data = gson.fromJson(jsonString, Something[].class);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question