Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson for beginners in Eclipse

let me just say that I'm still learning Java and the subtleties of Eclipse, and I come to you because I am unsure how to properly phrase my question to Google or to StackOverflow's search engine. Apologies if its infinitely trivial.

I am trying to understand the process of converting JSON-format strings into objects in Java. I found following example online:

import java.io.IOException;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;public class JsonToJavaConverter {

private static Logger logger = Logger.getLogger(JsonToJavaConverter.class);

public static void main(String args[]) throws JsonParseException,
        JsonMappingException, IOException {

    JsonToJavaConverter converter = new JsonToJavaConverter();

    String json = "{\n" + "    \"name\": \"Garima\",\n"
            + "    \"surname\": \"Joshi\",\n"
            + "    \"phone\": 9832734651}";

    // converting JSON String to Java object
    converter.fromJson(json);
}

public Object fromJson(String json) throws JsonParseException,
        JsonMappingException, IOException {
    User garima = new ObjectMapper().readValue(json, User.class);
    logger.info("Java Object created from JSON String ");
    logger.info("JSON String : " + json);
    logger.info("Java Object : " + garima);

    return garima;
}

public static class User {
    private String name;
    private String surname;
    private long phone;

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", surname=" + surname + ", phone="
                + phone + "]";
    }
}}

Now, here's the silly part (please don't string me up for asking):

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

Are all underlined in red with Eclipses only hint, being that "The import org.apache.log4j cannot be resolved".

As a newcomer to both Eclipse and Java, this leaves me absolutely dumbstruck.

Could anyone please tell me what needs doing to resolve this basic issue?

Deeply appreciated.

like image 669
ViRALiC Avatar asked Feb 07 '14 17:02

ViRALiC


People also ask

What is Jackson used for?

Jackson is a high-performance JSON processor used for Java. It is the most popular library used for serializing Java objects or Map to JSON and vice-versa. It is completely based on Java. Jackson tutorial provides all the basic and advanced concepts of the Jackson library.

What is Fasterxml Jackson used for?

jackson. databind. Basic data binding (mapping) functionality that allows for reading JSON content into Java Objects (POJOs) and JSON Trees ( JsonNode ), as well as writing Java Objects and trees as JSON.

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.


2 Answers

You need to add the libraries (jar files) to the project's build path in Eclipse.

You can find these libraries in Maven Central here:

Log4j

Jackson

like image 124
superEb Avatar answered Oct 30 '22 19:10

superEb


You need to add the relevant Jar files to your projects classpath

http://javahowto.blogspot.co.uk/2006/06/set-classpath-in-eclipse-and-netbeans.html

like image 30
M21B8 Avatar answered Oct 30 '22 21:10

M21B8