| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <classpath> | ||
| 3 | + <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/> | ||
| 4 | + <classpathentry kind="src" path="src/main/java" including="**/*.java"/> | ||
| 5 | + <classpathentry kind="output" path="target/classes"/> | ||
| 6 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| 7 | + <classpathentry kind="var" path="M2_REPO/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar"/> | ||
| 8 | + </classpath> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,24 @@ | |||
| 1 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | + <modelVersion>4.0.0</modelVersion> | ||
| 4 | + | ||
| 5 | + <groupId>com.hmkcode</groupId> | ||
| 6 | + <artifactId>gson-json-java</artifactId> | ||
| 7 | + <version>1.0-SNAPSHOT</version> | ||
| 8 | + <packaging>jar</packaging> | ||
| 9 | + | ||
| 10 | + <name>gson-json-java</name> | ||
| 11 | + <url>http://maven.apache.org</url> | ||
| 12 | + | ||
| 13 | + <properties> | ||
| 14 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 15 | + </properties> | ||
| 16 | + | ||
| 17 | + <dependencies> | ||
| 18 | + <dependency> | ||
| 19 | + <groupId>com.google.code.gson</groupId> | ||
| 20 | + <artifactId>gson</artifactId> | ||
| 21 | + <version>2.2.4</version> | ||
| 22 | + </dependency> | ||
| 23 | + </dependencies> | ||
| 24 | + </project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,55 @@ | |||
| 1 | + package com.hmkcode; | ||
| 2 | + | ||
| 3 | + import java.lang.reflect.Type; | ||
| 4 | + import java.util.LinkedList; | ||
| 5 | + import java.util.List; | ||
| 6 | + | ||
| 7 | + import com.google.gson.Gson; | ||
| 8 | + import com.google.gson.reflect.TypeToken; | ||
| 9 | + import com.hmkcode.vo.Article; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | + public class App | ||
| 13 | + { | ||
| 14 | + public static void main( String[] args ) | ||
| 15 | + { | ||
| 16 | + Gson gson = new Gson(); | ||
| 17 | + | ||
| 18 | + | ||
| 19 | + List<Article> articles = new LinkedList<Article>(); | ||
| 20 | + | ||
| 21 | + articles.add(createArticle()); | ||
| 22 | + articles.add(createArticle()); | ||
| 23 | + | ||
| 24 | + // Java --> JSON | ||
| 25 | + String json = gson.toJson(articles); | ||
| 26 | + System.out.println("toJson: "+json); | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + // JSON --> Java | ||
| 30 | + List list = gson.fromJson(json, List.class); | ||
| 31 | + System.out.println("fromJson: "+list); | ||
| 32 | + System.out.println("Class Type: "+list.get(0).getClass()); | ||
| 33 | + | ||
| 34 | + // JSON --> Java "Get the actual type" | ||
| 35 | + Type type = new TypeToken<List<Article>>(){}.getType(); | ||
| 36 | + list = gson.fromJson(json, type); | ||
| 37 | + System.out.println("fromJson: "+list); | ||
| 38 | + System.out.println("Class Type: "+list.get(0).getClass()); | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + } | ||
| 42 | + private static Article createArticle(){ | ||
| 43 | + | ||
| 44 | + Article article = new Article(); | ||
| 45 | + | ||
| 46 | + article.setTitle("GSON - Java JSON Library"); | ||
| 47 | + article.setUrl("http://hmkcode.com/gson-json-java"); | ||
| 48 | + article.addCategory("Java"); | ||
| 49 | + article.addTag("Java"); | ||
| 50 | + article.addTag("GSON"); | ||
| 51 | + article.addTag("JSON"); | ||
| 52 | + | ||
| 53 | + return article; | ||
| 54 | + } | ||
| 55 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,57 @@ | |||
| 1 | + package com.hmkcode.vo; | ||
| 2 | + | ||
| 3 | + import java.util.LinkedList; | ||
| 4 | + import java.util.List; | ||
| 5 | + | ||
| 6 | + public class Article { | ||
| 7 | + | ||
| 8 | + private String title; | ||
| 9 | + private String url; | ||
| 10 | + private List<String> categories; | ||
| 11 | + private List<String> tags; | ||
| 12 | + | ||
| 13 | + public String getTitle() { | ||
| 14 | + return title; | ||
| 15 | + } | ||
| 16 | + public void setTitle(String title) { | ||
| 17 | + this.title = title; | ||
| 18 | + } | ||
| 19 | + public String getUrl() { | ||
| 20 | + return url; | ||
| 21 | + } | ||
| 22 | + public void setUrl(String url) { | ||
| 23 | + this.url = url; | ||
| 24 | + } | ||
| 25 | + public List<String> getCategories() { | ||
| 26 | + return categories; | ||
| 27 | + } | ||
| 28 | + public void setCategories(List<String> categories) { | ||
| 29 | + this.categories = categories; | ||
| 30 | + } | ||
| 31 | + public List<String> getTags() { | ||
| 32 | + return tags; | ||
| 33 | + } | ||
| 34 | + public void setTags(List<String> tags) { | ||
| 35 | + this.tags = tags; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public void addCategory(String category){ | ||
| 39 | + if(this.categories == null) | ||
| 40 | + this.categories = new LinkedList<String>(); | ||
| 41 | + this.categories.add(category); | ||
| 42 | + } | ||
| 43 | + public void addTag(String tag){ | ||
| 44 | + if(this.tags == null) | ||
| 45 | + this.tags = new LinkedList<String>(); | ||
| 46 | + | ||
| 47 | + this.tags.add(tag); | ||
| 48 | + } | ||
| 49 | + @Override | ||
| 50 | + public String toString() { | ||
| 51 | + return "Article [title=" + title + ", url=" + url + ", categories=" | ||
| 52 | + + categories + ", tags=" + tags + "]"; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments