
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to parse a JSON without duplicate keys using Gson in Java?
A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. We can parse a JSON without duplicate keys using the TypeToken class. If we want to create a type literal for Map, we can create an empty anonymous inner class. If we try to insert a duplicate key, it will generate an error at runtime, "Exception in thread "main" com.google.gson.JsonSyntaxException: duplicate key"
Syntax
public class TypeToken<T> extends Object
Example
import java.lang.reflect.Type; import java.util.Map; import com.google.gson.*; import com.google.gson.reflect.TypeToken; public class JsonWithoutDuplicateKeysTest { public static void main(String args[]) throws Exception { String json = "{\"123\":\"abc\", \"124\":\"def\", \"125\":\"ghi\"}"; Gson gson = new GsonBuilder().setPrettyPrinting().create(); Type mapType = new TypeToken<Map<Integer, String>>() {}.getType(); Map<String, String> map = gson.fromJson(json, mapType); System.out.println(map); } }
Output
{123=abc, 124=def, 125=ghi}
- Related Articles
- How to parse a JSON to Gson Tree Model in Java?
- How to get all the keys of a JSON object using GSON in Java?
- How to read/parse JSON array using Java?
- How to convert Java object to JSON using GSON library?
- How to parse JSON in Java?
- How to parse a JSON string using Streaming API in Java?
- How to rename the properties of JSON using Gson in Java?
- How to pretty print JSON using the Gson library in Java?
- Convert a Map to JSON using the Gson library in Java?
- How to write a JSON string to file using the Gson library in Java?
- Convert JSON object to Java object using Gson library in Java?\n
- Convert Java object to JSON using the Gson library in Java?\n
- How to convert HASHMAP to JSON using GSON in Android?
- How to parse JSON input using Python?
- How to add/insert additional property to JSON string using Gson in Java?\n

Advertisements