AndroidNewbie
AndroidNewbie

Reputation: 669

Jackson or JSON Usage

I'am reading articles about them but it confuses me. What is the difference between the two?

Which is one is better to use?

I have my JSON..how can JACKSON Help me with my parsing?

Upvotes: 0

Views: 343

Answers (4)

Michał Ziober
Michał Ziober

Reputation: 38710

From Wikipedia about JSON:

JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML.

From Jackson home page: Jackson is a:

  • FAST (measured to be faster than any other Java json parser and data binder)
  • Streaming (reading, writing)
  • Zero-dependency (does not rely on other packages beyond JDK)
  • Powerful (full data binding for common JDK classes as well as any Java bean class,
  • Collection, Map or Enum), Configurable
  • Open Source (Apache License – or, until 2.1, alternatively LGPL)

Below you can find simple example how to deserialize your JSON data to Java POJO classes:

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        URL osurceUrl = new URL("http://app-dlslsg.azurewebsites.net/json/postList.php");

        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        PostList postList = mapper.readValue(osurceUrl, PostList.class);
        System.out.println(postList);
    }
}

class PostList {

    private List<Post> post;

    public List<Post> getPost() {
        return post;
    }

    public void setPost(List<Post> post) {
        this.post = post;
    }

    @Override
    public String toString() {
        return Joiner.on(System.getProperty("line.separator")).join(post);
    }
}

class Post {

    private int id;
    private String body;
    private String image;
    private Calendar stamp;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Calendar getStamp() {
        return stamp;
    }

    public void setStamp(Calendar stamp) {
        this.stamp = stamp;
    }

    @Override
    public String toString() {
        return "Post [id=" + id + ", body=" + body + ", image=" + image + ", stamp=" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(stamp.getTime()) + "]";
    }
}

Above program prints:

Post [id=101, body=google, image=http://app-dlslsg.azurewebsites.net/images/google.png, stamp=2013-11-18 12:12:02]
Post [id=61, body=facebook, image=http://app-dlslsg.azurewebsites.net/images/facebook.png, stamp=2013-11-16 13:28:35]
Post [id=111, body=Calendar, image=http://app-dlslsg.azurewebsites.net/images/ical.png, stamp=2013-11-18 12:12:14]
Post [id=121, body=Outlook, image=http://app-dlslsg.azurewebsites.net/images/outlook.png, stamp=2013-11-18 12:12:21]
Post [id=131, body=USG, image=http://app-dlslsg.azurewebsites.net/images/1472825_453923301384770_1942535278_n.jpg, stamp=2013-11-18 12:24:30]
Post [id=231, body=http://dlsu-usg.com/activities/dare-for-10-extended/

WE DARE YOU… To make a change in someone’s life now.

The Office of the Vice President for External Affairs and BLAZE 2013 brings you “DARE FOR TEN" EXTENDED!!!

Your TEN PESOS can make a d, image=http://app-dlslsg.azurewebsites.net/, stamp=2013-11-27 14:47:24]
Post [id=241, body=http://tours.wowbatangas.com/files/2011/01/IMG_6018.jpg, image=http://app-dlslsg.azurewebsites.net/, stamp=2014-01-03 16:06:31]
Post [id=251, body=iTRAVELpost, image=http://app-dlslsg.azurewebsites.net/images/ic_launcher-web.png, stamp=2014-01-10 08:53:19]

Upvotes: 0

Adam Radomski
Adam Radomski

Reputation: 2575

Jackson is a library that operate on JSON

JSON stands for javascript object notation and it is a data format

Upvotes: 1

user1596371
user1596371

Reputation:

JSON is a data format, Jackson is a Java library for creating and parsing JSON.

Upvotes: 1

Madhur Ahuja
Madhur Ahuja

Reputation: 22709

You cannot compare Jackson and JSON. Jackson is the library for processing JSON data.

Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.

http://wiki.fasterxml.com/JacksonHome

Upvotes: 1

Related Questions