Parse JSON in Java and Input to Mysql

Halo bro bro sekalian, sekarang saya akan mentutorialkan bagaimana cara parsing json ke java

ini File JSON nya
file tutor.json


{
“data”: [
{
“id”: “100000987654321_123456789190001”,
“from”: {
“name”: “Dpapayas”,
“id”: “100000987654321”
},
“message”: “Aku galau tanpamu hikz hikz hikz ….. “,
“type”: “status”,
“status_type”: “mobile_status_update”,
“application”: {
“name”: “Facebook for Android”,
“id”: “2254487659”
},
“created_time”: “2012-10-13T12:05:58+0000”,
“updated_time”: “2012-10-14T12:04:40+0000”,
“likes”: {
“data”: [
{
“name”: “Pacarnya Danang”,
“id”: “100000284817663”
}
],
“count”: 1
}
},
{
“id”: “768704935_10151197708124936”,
“from”: {
“name”: “Ujube Danang”,
“id”: “768704935”
},
“message”: “ih mas danang ganteng banget deh, jadi makin cinta deh “,
“type”: “status”,
“status_type”: “mobile_status_update”,
“created_time”: “2012-10-12T18:09:13+0000”,
“updated_time”: “2012-10-12T18:09:13+0000”
}

]

}

ini class ReadJSONFile2.Java
package coba;

/**
*
* @author Dpapayas
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class ReadJSONFile2 {

public static void main(String[] args) throws ClassNotFoundException, SQLException {

String myJSONString = “”;
BufferedReader bufferedReader = null;

Connection conn = null;
Statement st = null;
ResultSet rs = null;
String url = “jdbc:mysql://localhost:3306/”;
String db = “4lay”;
String driver = “com.mysql.jdbc.Driver”;
String usr = “root”;
String pass = “”;

Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url + db, usr, pass);
conn.setAutoCommit(false);
st = (Statement) conn.createStatement();

try {

String sCurrentLine;

bufferedReader = new BufferedReader(new FileReader(“E:\\!D/tutor.json”));

while ((sCurrentLine = bufferedReader.readLine()) != null) {
myJSONString = myJSONString + sCurrentLine;
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

Gson gson = new Gson();
FBUser fbUser = gson.fromJson(myJSONString, FBUser.class);

System.out.println(“FACEBOOK —>”);

ArrayList data = fbUser.getData();
for (int i = 0; i < data.size(); i++) {
System.out.println(“”);
System.out.println(“User ke ” + (i + 1));

Data data1 = data.get(i);

System.out.println(“id : ” + data1.getId());
if (data1.getFrom() == null) {
System.out.println(“From name : null”);
} else {
System.out.println(“from name : ” + data1.getFrom().getName());
}

System.out.println(“message : ” + data1.getMessage());
System.out.println(“type : ” + data1.getType());
System.out.println(“status_type : ” + data1.getStatus_type());

if (data1.getApplication() == null) {
System.out.println(“Application name : null”);
} else {
System.out.println(“Application name : ” + data1.getApplication().getName());
}
if (data1.getApplication() == null) {
System.out.println(“Application namespace : null”);
} else {
System.out.println(“Application namespace : ” + data1.getApplication().getNamespace());
}
if (data1.getApplication() == null) {
System.out.println(“Application id : null”);
} else {
System.out.println(“Application id : ” + data1.getApplication().getId());
}

System.out.println(“created_time : ” + data1.getCreated_time());
System.out.println(“updated_time : ” + data1.getUpdated_time());

if (data1.getLikes() == null) {
System.out.println(“likes count : null”);
} else {
System.out.println(“likes count : ” + data1.getLikes().getCount());
}

System.out.println(“”);

st.executeUpdate(“INSERT INTO facebook(NAME, MESSAGE, TYPE, TIME, STATUS_TYPE) VALUES(‘”+ data1.getFrom().getName() +”‘, ‘”+ data1.getMessage() +”‘ , ‘”+ data1.type +”‘, ‘”+data1.created_time +”‘, ‘”+ data1.getStatus_type() +”‘)”);

}

}

//Setters and Getters
}

nah udah selesai dan run filenya Shift + f6 tunggu berapa saat….

nah jadinya nanti seperti ini di console netbeans nya

nah dan database nya akan seperti ini

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s