50

Given an arbitrary JSON I would like to get value of a single field contentType. How to do it with Jackson?

{
  contentType: "foo",
  fooField1: ...
}

{
  contentType: "bar",
  barArray: [...]
}

Related

Community
  • 1
  • 1
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
  • Here https://stackoverflow.com/a/35722718/715269 is the way how to read only needed field in JSON without mapping it as a whole. – Gangnus Apr 28 '18 at 13:22
  • Since this question is still seeing activity nowadays, I thought it important to mention that @Gangnus's comment above is factually incorrect, as discussed in the comments to [my own answer below](https://stackoverflow.com/a/26191723/2427596) – ccjmne Feb 09 '22 at 12:24

5 Answers5

81

The Jackson Way

Presuming you don't have a POJO describing your data structure, you could simply do:

final String json = "{\"contentType\": \"foo\", \"fooField1\": ... }";
final JsonNode node = new ObjectMapper().readTree(json);
//                    ^^^^^^^^^^^^^^^^^^
// n.b.: try and *reuse* a single instance of ObjectMapper in production

if (node.has("contentType")) {
    System.out.println("contentType: " + node.get("contentType"));
}    

Addressing concerns in the comments section

If, however, you wish to not consume the entire source String, but simply access a specific property whose path you know, you'll have to write it yourself, leveraging a Tokeniser.


Actually, it's the weekend and I got time on my hands, so I could give you a head start: here's a basic one! It can run in strict mode and spew out sensible error messages, or be lenient and return Optional.empty when the request couldn't be fulfilled.

public static class JSONPath {

    protected static final JsonFactory JSON_FACTORY = new JsonFactory();

    private final List<JSONKey> keys;

    public JSONPath(final String from) {
        this.keys = Arrays.stream((from.startsWith("[") ? from : String.valueOf("." + from))
                .split("(?=\\[|\\]|\\.)"))
                .filter(x -> !"]".equals(x))
                .map(JSONKey::new)
                .collect(Collectors.toList());
    }

    public Optional<String> getWithin(final String json) throws IOException {
        return this.getWithin(json, false);
    }

    public Optional<String> getWithin(final String json, final boolean strict) throws IOException {
        try (final InputStream stream = new StringInputStream(json)) {
            return this.getWithin(stream, strict);
        }
    }

    public Optional<String> getWithin(final InputStream json) throws IOException {
        return this.getWithin(json, false);
    }

    public Optional<String> getWithin(final InputStream json, final boolean strict) throws IOException {
        return getValueAt(JSON_FACTORY.createParser(json), 0, strict);
    }

    protected Optional<String> getValueAt(final JsonParser parser, final int idx, final boolean strict) throws IOException {
        try {
            if (parser.isClosed()) {
                return Optional.empty();
            }

            if (idx >= this.keys.size()) {
                parser.nextToken();
                if (null == parser.getValueAsString()) {
                    throw new JSONPathException("The selected node is not a leaf");
                }

                return Optional.of(parser.getValueAsString());
            }

            this.keys.get(idx).advanceCursor(parser);
            return getValueAt(parser, idx + 1, strict);
        } catch (final JSONPathException e) {
            if (strict) {
                throw (null == e.getCause() ? new JSONPathException(e.getMessage() + String.format(", at path: '%s'", this.toString(idx)), e) : e);
            }

            return Optional.empty();
        }
    }

    @Override
    public String toString() {
        return ((Function<String, String>) x -> x.startsWith(".") ? x.substring(1) : x)
                .apply(this.keys.stream().map(JSONKey::toString).collect(Collectors.joining()));
    }

    private String toString(final int idx) {
        return ((Function<String, String>) x -> x.startsWith(".") ? x.substring(1) : x)
                .apply(this.keys.subList(0, idx).stream().map(JSONKey::toString).collect(Collectors.joining()));
    }

    @SuppressWarnings("serial")
    public static class JSONPathException extends RuntimeException {

        public JSONPathException() {
            super();
        }

        public JSONPathException(final String message) {
            super(message);
        }

        public JSONPathException(final String message, final Throwable cause) {
            super(message, cause);
        }

        public JSONPathException(final Throwable cause) {
            super(cause);
        }
    }

    private static class JSONKey {

        private final String key;
        private final JsonToken startToken;

        public JSONKey(final String str) {
            this(str.substring(1), str.startsWith("[") ? JsonToken.START_ARRAY : JsonToken.START_OBJECT);
        }

        private JSONKey(final String key, final JsonToken startToken) {
            this.key = key;
            this.startToken = startToken;
        }

        /**
         * Advances the cursor until finding the current {@link JSONKey}, or
         * having consumed the entirety of the current JSON Object or Array.
         */
        public void advanceCursor(final JsonParser parser) throws IOException {
            final JsonToken token = parser.nextToken();
            if (!this.startToken.equals(token)) {
                throw new JSONPathException(String.format("Expected token of type '%s', got: '%s'", this.startToken, token));
            }

            if (JsonToken.START_ARRAY.equals(this.startToken)) {
                // Moving cursor within a JSON Array
                for (int i = 0; i != Integer.valueOf(this.key).intValue(); i++) {
                    JSONKey.skipToNext(parser);
                }
            } else {
                // Moving cursor in a JSON Object
                String name;
                for (parser.nextToken(), name = parser.getCurrentName(); !this.key.equals(name); parser.nextToken(), name = parser.getCurrentName()) {
                    JSONKey.skipToNext(parser);
                }
            }
        }

        /**
         * Advances the cursor to the next entry in the current JSON Object
         * or Array.
         */
        private static void skipToNext(final JsonParser parser) throws IOException {
            final JsonToken token = parser.nextToken();
            if (JsonToken.START_ARRAY.equals(token) || JsonToken.START_OBJECT.equals(token) || JsonToken.FIELD_NAME.equals(token)) {
                skipToNextImpl(parser, 1);
            } else if (JsonToken.END_ARRAY.equals(token) || JsonToken.END_OBJECT.equals(token)) {
                throw new JSONPathException("Could not find requested key");
            }
        }

        /**
         * Recursively consumes whatever is next until getting back to the
         * same depth level.
         */
        private static void skipToNextImpl(final JsonParser parser, final int depth) throws IOException {
            if (depth == 0) {
                return;
            }

            final JsonToken token = parser.nextToken();
            if (JsonToken.START_ARRAY.equals(token) || JsonToken.START_OBJECT.equals(token) || JsonToken.FIELD_NAME.equals(token)) {
                skipToNextImpl(parser, depth + 1);
            } else {
                skipToNextImpl(parser, depth - 1);
            }
        }

        @Override
        public String toString() {
            return String.format(this.startToken.equals(JsonToken.START_ARRAY) ? "[%s]" : ".%s", this.key);
        }
    }
}

Assuming the following JSON content:

{
  "people": [{
    "name": "Eric",
    "age": 28
  }, {
    "name": "Karin",
    "age": 26
  }],
  "company": {
    "name": "Elm Farm",
    "address": "3756 Preston Street Wichita, KS 67213",
    "phone": "857-778-1265"
  }
}

... you could use my JSONPath class as follows:

    final String json = "{\"people\":[],\"company\":{}}"; // refer to JSON above
    System.out.println(new JSONPath("people[0].name").getWithin(json)); // Optional[Eric]
    System.out.println(new JSONPath("people[1].name").getWithin(json)); // Optional[Karin]
    System.out.println(new JSONPath("people[2].name").getWithin(json)); // Optional.empty
    System.out.println(new JSONPath("people[0].age").getWithin(json));  // Optional[28]
    System.out.println(new JSONPath("company").getWithin(json));        // Optional.empty
    System.out.println(new JSONPath("company.name").getWithin(json));   // Optional[Elm Farm]

Keep in mind that it's basic. It doesn't coerce data types (every value it returns is a String) and only returns leaf nodes.

Actual test case

It handles InputStreams, so you can test it against some giant JSON document and see that it's much faster than it would take your browser to download and display its contents:

System.out.println(new JSONPath("info.contact.email")
            .getWithin(new URL("http://test-api.rescuegroups.org/v5/public/swagger.php").openStream()));
// Optional[support@rescuegroups.org]

Quick test

Note I'm not re-using any already existing JSONPath or ObjectMapper so the results are inaccurate -- this is just a very rough comparison anyways:

public static Long time(final Callable<?> r) throws Exception {
    final long start = System.currentTimeMillis();
    r.call();
    return Long.valueOf(System.currentTimeMillis() - start);
}

public static void main(final String[] args) throws Exception {
    final URL url = new URL("http://test-api.rescuegroups.org/v5/public/swagger.php");
    System.out.println(String.format(   "%dms to get 'info.contact.email' with JSONPath",
                                        time(() -> new JSONPath("info.contact.email").getWithin(url.openStream()))));
    System.out.println(String.format(   "%dms to just download the entire document otherwise",
                                        time(() -> new Scanner(url.openStream()).useDelimiter("\\A").next())));
    System.out.println(String.format(   "%dms to bluntly map it entirely with Jackson and access a specific field",
                                        time(() -> new ObjectMapper()
                                                .readValue(url.openStream(), ObjectNode.class)
                                                .get("info").get("contact").get("email"))));
}

378ms to get 'info.contact.email' with JSONPath
756ms to just download the entire document otherwise
896ms to bluntly map it entirely with Jackson and access a specific field

ccjmne
  • 9,333
  • 3
  • 47
  • 62
  • 1
    The question was how to read a single field. Mapping the whole object is absolutely excessive for that. The answer is NOT to the question. – Gangnus Apr 28 '18 at 12:25
  • 1
    The another answer is the correct one. Jackson has two regimes: reading fields separately or mapping the whole object. The question is about the first one. – Gangnus Apr 28 '18 at 12:55
  • @Gangnus: Nice! Where can I read about that? I can't seem to find any info after a quick Google Search... I don't think the second answer mentions `Jackson`, which is the library asked for in the question. Nor does it explain the difference you seem to find! – ccjmne Apr 28 '18 at 12:59
  • http://www.studytrails.com/java/json/java-jackson-introduction/. Or look for JSONObject type - it is just the class that allows you to read only that part of json that you need. You are right - a sensible search for "reading separate field in jackson" does not work. – Gangnus Apr 28 '18 at 13:05
  • JSONObject reads the json string without using any POJO objects. – Gangnus Apr 28 '18 at 13:09
  • I just read the code of the `JSONObject` class and I guarantee you that the code featured in the other answer does indeed map the entire `String` (no different from my answer). You are wrong. At least when used with the JSONObject(String) constructor, followed by JSONObject#getString, which stores all parsed properties into a Map and simply gets one from there. – ccjmne Apr 28 '18 at 13:09
  • Maybe it maps the json somewhere, but even so you don't need to create maybe very complex set of annotated POJOs to read a field. I would be even more happy if there is a possibility to leave json as string and to decifer only its needed part. – Gangnus Apr 28 '18 at 13:18
  • I have recalled and found the correct way, and added the comment to the question. – Gangnus Apr 28 '18 at 13:22
  • @Gangnus, The solution I showed simply uses an `ObjectNode`, which is back by a simple `LinkedHashMap`! That's it's ONLY field and it is NOT annotated! There's actually **no** difference :) – ccjmne Apr 28 '18 at 13:22
  • 1
    @Gangnus: You got me interested in this problem. It turns out that the answer you linked in the comments section does exactly what you don't want! It ultimately delegates to `mapper.readValue(jp, Foo.class)`, where `Foo` is a POJO that is annotated... I couldn't seem to find a full-blown solution that actually lets you access a specific field without first parsing the entire contents... so I wrote one. I thought you might be interested! Cheers. – ccjmne Apr 28 '18 at 22:15
28

Just want to update for 2019. I found the following easiest to impl:

//json can be file or String
JsonNode parent= new ObjectMapper().readTree(json);
String content = parent.path("contentType").asText();

I would suggest to use path instead of get as get throws a NPE, where path returns with a default 0 or "", which is safer to work with if setting up the parsing correctly for 1st time.

My $0.02

Rohan Kumar
  • 726
  • 10
  • 17
7

If you are using JSON jars in your application then the following code snippet is useful:

String json = "{\"contentType\": \"foo\", \"fooField1\": ... }";
JSONObject jsonObject = new JSONObject(json);
System.out.println(jsonObject.getString("contentType"));

and if you are using Gson jars then the same code will look like following:

Gson gson = new GsonBuilder().create();
Map jsonMap = gson.fromJson(json, Map.class);
System.out.println(jsonMap.get("contentType"));
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
5

Another way is:

String json = "{\"contentType\": \"foo\", \"fooField1\": ... }";
JsonNode parent= new ObjectMapper().readTree(json);
String content = parent.get("contentType").asText();
xyz
  • 812
  • 9
  • 18
0

I faced this issue when I decided to use Jackson as the json library for a project I worked on; mainly for its speed. I was already used to using org.json and Gson for my projects.

I quickly found out though that many tasks that were trivial in org.json and Gson were not so straightforward in Jackson

So I wrote the following classes to make things easier for me.

The classes below will allow you to use Jackson as easily as you would the simple org.json library, while still retaining the power and speed of Jackson

I wrote the whole thing in a few hours, so feel free to debug and suit the code to your own purposes.

Note that JSONObject/JSONArray below will do exactly what the OP wants.

The first is JSONObject which has similar methods to org.json.JSONObject; but at heart runs Jackson code to build JSON and parse json strings.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author GBEMIRO JIBOYE <gbenroscience@gmail.com>
 */
public class JSONObject {

    ObjectNode parseNode;

    public JSONObject() {
        this.parseNode = JsonNodeFactory.instance.objectNode(); // initializing     
    }

    public JSONObject(String json) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        try {

            this.parseNode = mapper.readValue(json, ObjectNode.class);

        } catch (JsonProcessingException ex) {
            Logger.getLogger(JSONObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void put(String key, String value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, boolean value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, int value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, short value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, float value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, long value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, double value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, byte[] value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, BigInteger value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, BigDecimal value) {
        parseNode.put("key", value); // building
    }

    public void put(String key, Object[] value) {
        ArrayNode anode = parseNode.putArray(key);
        for (Object o : value) {
            anode.addPOJO(o); // building 
        }
    }

    public void put(String key, JSONObject value) {
        parseNode.set(key, value.parseNode);
    }

    public void put(String key, Object value) {
        parseNode.putPOJO(key, value);
    }

    public static class Parser<T> {

        public T decode(String json, Class clazz) {
            try {
                return new Converter<T>().fromJsonString(json, clazz);
            } catch (IOException ex) {
                Logger.getLogger(JSONObject.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

    }

    public int optInt(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null ? nod.asInt(0) : 0;
        }
        return 0;
    }

    public long optLong(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null ? nod.asLong(0) : 0;
        }
        return 0;
    }

    public double optDouble(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null ? nod.asDouble(0) : 0;
        }
        return 0;
    }

    public boolean optBoolean(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null ? nod.asBoolean(false) : false;
        }
        return false;
    }

    public double optFloat(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null && nod.isFloat() ? nod.floatValue() : 0;
        }
        return 0;
    }

    public short optShort(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return nod != null && nod.isShort() ? nod.shortValue() : 0;
        }
        return 0;
    }

    public byte optByte(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);

            return nod != null && nod.isShort() ? (byte) nod.asInt(0) : 0;

        }
        return 0;
    }

    public JSONObject optJSONObject(String key) {
        if (parseNode != null) {
            if (parseNode.has(key)) {
                ObjectNode nod = parseNode.with(key);

                JSONObject obj = new JSONObject();
                obj.parseNode = nod;

                return obj;
            }

        }
        return new JSONObject();
    }

    public JSONArray optJSONArray(String key) {
        if (parseNode != null) {
            if (parseNode.has(key)) {
                ArrayNode nod = parseNode.withArray(key);

                JSONArray obj = new JSONArray();
                if (nod != null) {
                    obj.parseNode = nod;
                    return obj;
                }
            }

        }
        return new JSONArray();
    }

    public String optString(String key) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(key);
            return parseNode != null && nod.isTextual() ? nod.asText("") : "";
        }
        return "";
    }

    @Override
    public String toString() {
        return parseNode.toString();
    }

    public String toCuteString() {
        return parseNode.toPrettyString();
    }

}

Here is the code for the JSONArray equivalent that works like org.json.JSONArray; but uses Jackson code.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.math.BigDecimal;
import java.math.BigInteger; 

/**
 *
 * @author GBEMIRO JIBOYE <gbenroscience@gmail.com>
 */
public class JSONArray {


    protected ArrayNode parseNode; 

    public JSONArray() {
        this.parseNode = JsonNodeFactory.instance.arrayNode(); // initializing     
    }

    public JSONArray(String json) throws JsonProcessingException{
        ObjectMapper mapper = new ObjectMapper();

            this.parseNode = mapper.readValue(json, ArrayNode.class);

    }

    public void putByte(byte val) {
        parseNode.add(val);
    }

    public void putShort(short val) {
        parseNode.add(val);
    }

    public void put(int val) {
        parseNode.add(val);
    }

    public void put(long val) {
        parseNode.add(val);
    }

    public void pu(float val) {
        parseNode.add(val);
    }

    public void put(double val) {
        parseNode.add(val);
    }

    public void put(String val) {
        parseNode.add(val);
    }

    public void put(byte[] val) {
        parseNode.add(val);
    }

    public void put(BigDecimal val) {
        parseNode.add(val);
    }

    public void put(BigInteger val) {
        parseNode.add(val);
    }

    public void put(Object val) {
        parseNode.addPOJO(val);
    }

     public void put(int index, JSONArray value) {
        parseNode.set(index, value.parseNode);
    }

      public void put(int index, JSONObject value) {
        parseNode.set(index, value.parseNode);
    }

     public String optString(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null ? nod.asText("") : "";
        }
        return "";
    }
 public int optInt(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null ? nod.asInt(0) : 0;
        }
        return 0;
    }

    public long optLong(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null ? nod.asLong(0) : 0;
        }
        return 0;
    }

    public double optDouble(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null ? nod.asDouble(0) : 0;
        }
        return 0;
    }

    public boolean optBoolean(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null ? nod.asBoolean(false) : false;
        }
        return false;
    }

    public double optFloat(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null && nod.isFloat() ? nod.floatValue() : 0;
        }
        return 0;
    }

    public short optShort(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);
            return nod != null && nod.isShort() ? nod.shortValue() : 0;
        }
        return 0;
    }

    public byte optByte(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);

            return nod != null && nod.isShort() ? (byte) nod.asInt(0) : 0;

        }
        return 0;
    }

    public JSONObject optJSONObject(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);

            if(nod != null){
                if(nod.isObject()){
                        ObjectNode obn = (ObjectNode) nod;
                          JSONObject obj = new JSONObject();
                          obj.parseNode = obn;
                          return obj;
                   }

            }

        }
        return new JSONObject();
    }

      public JSONArray optJSONArray(int index) {
        if (parseNode != null) {
            JsonNode nod = parseNode.get(index);

            if(nod != null){
                if(nod.isArray()){
                        ArrayNode anode = (ArrayNode) nod;
                          JSONArray obj = new JSONArray();
                          obj.parseNode = anode;
                          return obj;
                   }

            }

        }
        return new JSONArray();
    }

         @Override
    public String toString() {
        return parseNode.toString();
    }
      public String toCuteString() {
        return parseNode.toPrettyString();
    }


}

Finally for a one size-fits-all-most-likely for encoding and decoding your Java classes to JSON, I added this simple class:

/**
 *
 * @author GBEMIRO JIBOYE <gbenroscience@gmail.com>
 */
public class Converter<T> {
    // Serialize/deserialize helpers

    private Class clazz;

    public Converter() {}

    public T fromJsonString(String json , Class clazz) throws IOException {
        this.clazz = clazz;
        return getObjectReader().readValue(json);
    }

    public String toJsonString(T obj) throws JsonProcessingException {
        this.clazz = obj.getClass();
        return getObjectWriter().writeValueAsString(obj);
    }

    private ObjectReader requestReader;
    private ObjectWriter requestWriter;

    private void instantiateMapper() {
        ObjectMapper mapper = new ObjectMapper()
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);



        requestReader = mapper.readerFor(clazz);
        requestWriter = mapper.writerFor(clazz);
    }

    private ObjectReader getObjectReader() {
        if (requestReader == null) {
            instantiateMapper();
        }
        return requestReader;
    }

    private ObjectWriter getObjectWriter() {
        if (requestWriter == null) {
            instantiateMapper();
        }
        return requestWriter;
    }



}

Now to taste(test) the sauce(code), use the following methods:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author GBEMIRO JIBOYE <gbenroscience@gmail.com>
 */
public class SimplerJacksonTest {


    static class Credentials {

        private String userName;
        private String uid;
        private String password;
        private long createdAt;

        public Credentials() {
        }

        public Credentials(String userName, String uid, String password, long createdAt) {
            this.userName = userName;
            this.uid = uid;
            this.password = password;
            this.createdAt = createdAt;
        }

        @JsonProperty("userName")
        public String getUserName() {
            return userName;
        }

        @JsonProperty("userName")
        public void setUserName(String userName) {
            this.userName = userName;
        }

        @JsonProperty("uid")
        public String getUid() {
            return uid;
        }

        @JsonProperty("uid")
        public void setUid(String uid) {
            this.uid = uid;
        }

        @JsonProperty("password")
        public String getPassword() {
            return password;
        }

        @JsonProperty("password")
        public void setPassword(String password) {
            this.password = password;
        }

        @JsonProperty("createdAt")
        public long getCreatedAt() {
            return createdAt;
        }

        @JsonProperty("createdAt")
        public void setCreatedAt(long createdAt) {
            this.createdAt = createdAt;
        }

        public String encode() {
            try {
                return new Converter<Credentials>().toJsonString(this);
            } catch (JsonProcessingException ex) {
                Logger.getLogger(Credentials.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

        public Credentials decode(String jsonData) {
            try {
                return new Converter<Credentials>().fromJsonString(jsonData, Credentials.class);
            } catch (Exception ex) {
                Logger.getLogger(Converter.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }

    }


    public static JSONObject testJSONObjectBuild() {
        JSONObject obj = new JSONObject();

        Credentials cred = new Credentials("Adesina", "01eab26bwkwjbak2vngxh9y3q6", "xxxxxx1234", System.currentTimeMillis());
        String arr[] = new String[]{"Boy", "Girl", "Man", "Woman"};
        int nums[] = new int[]{0, 1, 2, 3, 4, 5};

        obj.put("creds", cred);
        obj.put("pronouns", arr);
        obj.put("creds", cred);
        obj.put("nums", nums);

        System.out.println("json-coding: " + obj.toCuteString());

        return obj;
    }

    public static void testJSONObjectParse(String json) {
        JSONObject obj;
        try {
            obj = new JSONObject(json);

            JSONObject credsObj = obj.optJSONObject("creds");

            String userName = credsObj.optString("userName");
            String uid = credsObj.optString("uid");
            String password = credsObj.optString("password");
            long createdAt = credsObj.optLong("createdAt");


            System.out.println("<<---Parse Results--->>");
            System.out.println("userName = " + userName);
            System.out.println("uid = " + uid);
            System.out.println("password = " + password);
            System.out.println("createdAt = " + createdAt);


        } catch (JsonProcessingException ex) {
            Logger.getLogger(JSONObject.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

       public static JSONArray testJSONArrayBuild() {

         JSONArray array = new JSONArray();
        array.put(new Credentials("Lawani", "001uadywdbs", "ampouehehu", System.currentTimeMillis()));

        array.put("12");
        array.put(98);
        array.put(Math.PI);
        array.put("Good scores!");

        System.out.println("See the built array: "+array.toCuteString());

        return array;

    }


       public static void testJSONArrayParse(String json) {

        try {
            JSONArray array = new JSONArray(json);


            JSONObject credsObj = array.optJSONObject(0);

            //Parse credentials in index 0

            String userName = credsObj.optString("userName");
            String uid = credsObj.optString("uid");
            String password = credsObj.optString("password");
            long createdAt = credsObj.optLong("createdAt");


            //Now return to the  main array and parse other entries

            String twelve = array.optString(1);
            int ninety = array.optInt(2);
            double pi = array.optDouble(3);
            String scoreNews = array.optString(4);


            System.out.println("Parse Results");
            System.out.println("userName = " + userName);
            System.out.println("uid = " + uid);
            System.out.println("password = " + password);
            System.out.println("createdAt = " + createdAt);


            System.out.println("Parse Results");
            System.out.println("index 1 = " + twelve);
            System.out.println("index 2 = " + ninety);
            System.out.println("index 3 = " + pi);
            System.out.println("index 4 = " + scoreNews);


        } catch (JsonProcessingException ex) {
            Logger.getLogger(JSONObject.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


       public static String testCredentialsEncode(){
            Credentials cred = new Credentials("Olaoluwa", "01eab26bwkwjbak2vngxh9y3q6", "xxxxxx1234", System.currentTimeMillis());

            String encoded = cred.encode();
            System.out.println("encoded credentials = "+encoded);
            return encoded;
       }

        public static Credentials testCredentialsDecode(String json){
            Credentials cred = new Credentials().decode(json);


            System.out.println("encoded credentials = "+cred.encode());


            return cred;
       }


    public static void main(String[] args) {

       JSONObject jo = testJSONObjectBuild();
        testJSONObjectParse(jo.toString());

        JSONArray ja = testJSONArrayBuild();

        testJSONArrayParse(ja.toString());

        String credsJSON = testCredentialsEncode();

        testCredentialsDecode(credsJSON);

    }

}

To get the source code in a place, instead of having to copy the one here, see:

the code on Github

gbenroscience
  • 994
  • 2
  • 10
  • 33