Knowledge Walls
John Peter
Pune, Maharashtra, India
How to Convert JSON String to Java Object in Core Java with Example
3216 Views
Hints 
Gson is a google external library. Gson handles JSON string to Java Object conversion uses fromJson() method to parse the string information to java object.

Example
new Gson().fromJson(<JSON String>,<Java Object Type>);
JSONStringToJavaObject
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;


public class JSONStringToJavaObject {
    public static void main(String args[]) throws Exception {
        String jsonString = "['Raja','Kumar','Vijay','Sam','Guru']";
        List<String> nameList = new ArrayList<String>();

        Type jsonObjectType = new TypeToken<List<String>>() {}.getType();
        nameList = new Gson().fromJson(jsonString, jsonObjectType);

        System.out.println(nameList);
    }
}
Output 
[Raja, Kumar, Vijay, Sam, Guru]
Previous Topics
Previous lessons of current book.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details