org.json

Salah satu java library untuk parsing JSON adalah org.json. Penggunaannya adalah sebagai berikut:

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONTokener;

String strJSON = "
{"error_code":0,"error_desc":"","data":[
{"nama_mahasiswa":"IRMAYANTI","jenis_kelamin":"P","tanggal_lahir":"1975-11-11T17:00:00.000Z"},
{"nama_mahasiswa":"FAUZIYAH","jenis_kelamin":"P","tanggal_lahir":"1975-12-20T17:00:00.000Z"}]}
";
    
JSONTokener tokener = new JSONTokener(strJSON);
JSONObject object = new JSONObject(tokener);
System.out.println(object.getInt("error_code"));
System.out.println(object.getString("error_desc"));
JSONArray data = object.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
  nama_mahasiswa  = (data.getJSONObject(i).getString("nama_mahasiswa"));
  System.out.println(nama_mahasiswa);
}

String strJSON = "
{"error_code":0,"error_desc":"","data":{
"token":"1a513fbb-34db-49c3-a7f4-b0d6b4383877"}}
";
        
JSONTokener tokener = new JSONTokener(strToken);
JSONObject object = new JSONObject(tokener);   System.out.println(object.getJSONObject("data").getString("token"));
# mongodb

{"files":{"data":[
{"_id":{"$oid":"637d0799f79eb6bab4957150"}, "files_name":"G4_Proposal", "files_category":"proposal", "files_keyword":"workshop, proposal, student, project"},
{"_id":{"$oid":"637d0868f79eb6bab4957151"}, "files_name":"CV_Intern_Adella", "files_category":"intern student","files_keyword":"cv, intern, job, experience, name, matric, university"}
],"status":"OK"}}

try {
            JSONTokener tokener = new JSONTokener(strJSON);
            JSONObject jsonObj = new JSONObject(tokener);
            Log.v("result", jsonObj.getString("files"));

            JSONTokener tokener1 = new JSONTokener(jsonObj.getString("files"));
            JSONObject jsonObj1 = new JSONObject(tokener1);
            Log.v("result", jsonObj1.getString("status"));
            JSONArray data = jsonObj1.getJSONArray("data");

            // looping through All
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);
                String files_name = c.getString("files_name");
                Log.v("result", files_name);
            }
        } catch (final JSONException e) { ...

Read by Python

import json

# some JSON:
x='{"files":{"data":[{"files_id":"1","files_name":"G4_Proposal","files_category":"proposal","files_keyword":"workshop, proposal, student, project"},{"files_id":"2","files_name":"CV_Intern_Adella","files_category":"intern student","files_keyword":"cv, intern, job, experience, name, matric, university"},{"files_id":"3","files_name":"fuzzyLogic_Adella","files_category":"exam","files_keyword":"exam, fuzzy, logic"}],"status":"OK"}}'

# parse x:
y = json.loads(x)
data = y["files"]
for i in data['data']:
    print(i['files_name'], i["files_category"], i["files_keyword"], )

# output:
G4_Proposal proposal workshop, proposal, student, project
CV_Intern_Adella intern student cv, intern, job, experience, name, matric, university
fuzzyLogic_Adella exam exam, fuzzy, logic

Leave a comment

I’m Eddy

Dive into my world where every page is a celebration of knowledge, particularly my fascination with computer science. Join me in this journey of exploration and learning—save the day by bookmarking knowledge that spans the breadth of computer science. Enjoy the read, and may each visit offer you something valuable to take away!


Let’s connect