How do i parse large json files with json.simple? - java

I am new to JSON and I am learning using the json.simple library. I try to parse a large json file, but all i get is “OutOfMemoryError: GC overhead limit exceeded”. How can i parse large json files?
this is my code:
private static void fillPublications() throws FileNotFoundException, IOException, SQLException {
JSONParser parser = new JSONParser();
try {
String username = System.getProperty("user.name");
String filename = "Datenbank";
Object jsonData = parser.parse(new FileReader("C:\\Users\\" + username + "\\Desktop\\JSON\\" + filename + ".json"));
ArrayList<String[]> list = new ArrayList<String[]>();
JSONArray jsonArray = (JSONArray) jsonData;
Iterator iterator = jsonArray.iterator();
String name = "";
Integer count = 0;
while (iterator.hasNext()) {
String[] database = new String[11];
JSONObject obj = (JSONObject) iterator.next();
if(obj.containsKey("title")){
database[0] = (String) obj.get("title");
}
if(obj.containsKey("year")){
database[1] = obj.get("year").toString();
}
if(obj.containsKey("keywords")){
JSONArray keywords = (JSONArray) obj.get("keywords");
Iterator<String> i = keywords.iterator();
String key = "";
while (i.hasNext()) {
key = key + i.next() + "#";
}
if(key.length() > 0 && key.charAt(key.length()-1)=='#'){
key = key.substring(0, key.length()-1);
}
database[2] = key;
}
if(obj.containsKey("pages")){
database[3] = (String) obj.get("pages");
}
if(obj.containsKey("volume")){
database[4] = (String) obj.get("volume");
}
if(obj.containsKey("abstract")){
database[5] = (String) obj.get("abstract");
}
if(obj.containsKey("identifiers")){
JSONObject identifiers = (JSONObject) obj.get("identifiers");
database[6] = (String) identifiers.get("doi");
}
if(obj.containsKey("type")){
database[8] = (String) obj.get("type");
}
if(obj.containsKey("authors")){
JSONArray arr = (JSONArray) obj.get("authors");
Iterator i = arr.iterator();
while(i.hasNext()){
JSONObject authorObject = (JSONObject) i.next();
name = name + (String) authorObject.get("first_name") + " " + (String) authorObject.get("last_name") + "#";
}
if(name.length() > 0 && name.charAt(name.length()-1)=='#'){
name = name.substring(0, name.length()-1);
}
database[10] = name;
}
list.add(database);
}
System.out.println(Arrays.toString(list.get(0)));
}catch (Exception e) {
System.err.println(e);
}
}
and this is what i get:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.lang.StringBuffer.toString(StringBuffer.java:673)
at org.json.simple.parser.Yylex.yylex(Yylex.java:598)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
at JSONExtraction.fillAuthors(JSONExtraction.java:39)
at JSONExtraction.main(JSONExtraction.java:23)
C:\Users\b\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1

Related

Get the Data from a Json String in codename one

am getting a json from the server and i managed to save it into storage,
{"FirstName":"Festus","MiddleName":"Kilaka","LastName":"Mutinda","MemberNo":"1886","IdNumber":"3125638","PayRollNumber":"87","PhoneNumber":"254743901110","EmailAddress":"FESTUS#GMAIL.COM"}
all i need is to read through it and get each variable and attach them to my strings.
i tried the code below but am getting an error.
private String str_response;
private String memberNoLabel = "";
private String nameLabel = "";
private String idNoLbl = "";
private String staffNoLbl = "";
private String emailAddLbl = "";
private String PhoneNumber = ""; //Storage.getInstance().readObject("PhoneNumber").toString();
private String AccBalanceLable = "50,500";
str_response = (String) Storage.getInstance().readObject("profile.json");
Log.p("Reading profile.json from storage.", 1);
byte[] bytes_mydetails = str_response.getBytes();
JSONParser json_mysaccos = new JSONParser();
try (Reader is_mysaccos = new InputStreamReader(new ByteArrayInputStream(bytes_mydetails), "UTF-8"))
{
Map<String, Object> map_saccos = json_mysaccos.parseJSON(is_mysaccos);
java.util.List<Map<String, Object>> objMydetails = (java.util.List<Map<String, Object>>) map_saccos.get("MemberDetails");
System.out.println("No of Details: ---------> " + objMydetails.size());
for (Map<String, Object> objDetails : objMydetails)
{
String name1 = (String) objDetails.get("FirstName");
String name2 = (String)objDetails.get("MiddleName");
String name3 = (String)objDetails.get("LastName");
nameLabel = name1+name2+name3;
memberNoLabel = (String)objDetails.get("MemberNo");
idNoLbl = (String)objDetails.get("IdNumber");
staffNoLbl = (String)objDetails.get("PayRollNumber");
PhoneNumber = (String)objDetails.get("PhoneNumber");
emailAddLbl = (String)objDetails.get("EmailAddress");
}
}
catch (Exception e)
{
System.out.println("Error # saccos: " + e);
e.printStackTrace();
}
am getting this error
Error # saccos: java.lang.NullPointerException
java.lang.NullPointerException
and it says its from the line
System.out.println("No of Details: ---------> " + objMydetails.size());
have tried all available documents and tutorials i could get, but i cant figure out this, help...

JSONArray for array of String giving A JSONObject text must begin with '{' at 1 [character 2 line 1]

I am reading a JSON string and trying to get value for a given key.
code to get value from JSON
private static void getKey(JSONObject jsonObject, String key) {
boolean exist = jsonObject.has(key);
Iterator<?> keys;
String nextKey;
if(!exist) {
keys = jsonObject.keys();
while (keys.hasNext()) {
nextKey = (String) keys.next();
System.out.println("Key is :: " + nextKey);
try {
if(jsonObject.get(nextKey) instanceof JSONObject) {
if(!exist) {
getKey(jsonObject.getJSONObject(nextKey), key);
}
} else if(jsonObject.get(nextKey) instanceof JSONArray) {
JSONArray jsonArray = jsonObject.getJSONArray(nextKey);
for(int i = 0; i < jsonArray.length(); i++) {
String arrayValue = jsonArray.get(i).toString();
JSONObject innerJSONObject = new JSONObject(arrayValue.trim());
if(!exist) {
getKey(innerJSONObject, key);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
parseJSONObject(jsonObject, key);
}
}
I am trying to parse this JSON String
String data = "[{\n" +
"\t\"siteName\": \"tvt-ieee\",\n" +
"\t\"personId\": \"43038888\",\n" +
"\t\"editorId\": \"43038888-tvt-ieee\",\n" +
"\t\"emails\": [{\n" +
"\t\t\"emailAddress\": \"02superjh#gmail.com\"\n" +
"\t}],\n" +
"\t\"keywords\": [\"H.1.2 User/Machine Systems\",\"I.2 Artificial Intelligence\"],\n" +
"}]";
JSONArray jsonObject = new JSONArray(data);
getKey(jsonObject.getJSONObject(0), "keywords");
but when I am trying to get value for key "keywords" it is throwing an error like
A JSONObject text must begin with '{' at 1 [character 2 line 1]
please help me how can I solve this issue?
Thank you in advance

return sms to persian language in Java

Im using API for sending SMS of product query and taking variable from properties file in java.
I need to convert the output message to utf-8 as i need to send SMS of Persian language, please help me.
here is the code:
List<String> returnMessage = new ArrayList<String>();
Properties messagePack = new Properties();
messagePack.load(RoshanSendSMS.class.getClassLoader().getResourceAsStream(String.format("%spack.properties", language)));
JSONObject parentObject = new JSONObject(result);
JSONArray objArray = (JSONArray) parentObject.get("PurchasedOfferArray");
int size = objArray.length();
for (int i = 0; i < size; i++) {
JSONObject obj = (JSONObject) objArray.get(i);
if (!obj.has("CatalogItemId")) {
continue;
}
String catalogId = obj.get("CatalogItemId").toString();
String productId = messagePack.getProperty(catalogId);
if (productId == null) {
continue;
}
String newProductId = "ProductName" + productId.split("t")[1];
String msg = messagePack.getProperty(newProductId);
if (msg == null) {
continue;
}
if (!returnMessage.contains(msg)) {
returnMessage.add(msg);
}
}
if(returnMessage.size()>0){
returnMessage.add(0, messagePack.getProperty("PRE_MSG"));
String fullMsg = String.join(" ", returnMessage);
System.out.println("Message: "+ fullMsg);
}else{
System.out.println("No message found");
}
return "SUCCESS";

JSON Object Android placing issue

I am trying to place this JSON Object in my Listview in android but I am having a problem with json mismatch.
This is the output of my server JSON :
{"Patient 0":
{"firstName":"admir","lastName":"andrade","email":"admirmonteiro#live.com"},
"Patient 1":
{"firstName":"alden","lastName":"andrade","email":"admirmonteiro#yahoo.com"}}
In my android code, I have the following code :
JSONObject object = new JSONObject(result);
How can I updated my list with both patients information without having to call each object once(if there are many, I don't know how to do this)? What do I need to do further to update my listView with two columns, one for first name and last name and the other with the email, as the JSON output form the server show? Is there a better way to do this?
This works fine,
JSONObject jObject = new JSONObject(result.trim());
Iterator<?> keys = jObject.keys();
while(keys.hasNext() ) {
String key = (String)keys.next();
if (jObject.get(key) instanceof JSONObject ) {
String firstName = ((JSONObject) jObject.get(key)).getString("firstName");
String lastName = ((JSONObject) jObject.get(key)).getString("lastName");
String email = ((JSONObject) jObject.get(key)).getString("email");
Log.d("jsondata","Firstname = "+firstName+"\nlastname = "+lastName+"\nemail = "+email);
}
}
Update
Here is the full codes that I tested,
String result = "{\"Patient 0\":{\"firstName\":\"admir\",\"lastName\":\"andrade\",\"email\":\"admirmonteiro#live.com\"},\"Patient 1\":{\"firstName\":\"alden\",\"lastName\":\"andrade\",\"email\":\"admirmonteiro#yahoo.com\"}}";
JSONObject jObject = null;
try {
jObject = new JSONObject(result.trim());
Iterator<?> keys = jObject.keys();
while(keys.hasNext() ) {
String key = (String)keys.next();
if (jObject.get(key) instanceof JSONObject ) {
String firstName = ((JSONObject) jObject.get(key)).getString("firstName");
String lastName = ((JSONObject) jObject.get(key)).getString("lastName");
String email = ((JSONObject) jObject.get(key)).getString("email");
Log.d("jsondata","Firstname = "+firstName+"\nlastname = "+lastName+"\nemail = "+email);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
You can try this..
String result ="{\"Patient 0\":\n" +
"{\"firstName\":\"admir\",\"lastName\":\"andrade\",\"email\":\"admirmonteiro#live.com\"},\n" +
" \"Patient 1\":\n" +
"{\"firstName\":\"alden\",\"lastName\":\"andrade\",\"email\":\"admirmonteiro#yahoo.com\"}}";
JSONObject data = null;
try {
data = new JSONObject(""+result);
} catch (JSONException e) {
e.printStackTrace();
}
List<String> FName = new ArrayList<String>();
for(int i=0 ; i<data.length();i++){
try {
JSONObject innerData = data.getJSONObject("Patient "+i);
FName .add(innerData.getString("firstName"));
} catch (JSONException e) {
e.printStackTrace();
}
}

Trying to parse JSON in Android

How would you suggest me to parse this JSON:
{"17": {"user_name": "test1"}, "18": {"user_name": "test5"}, "19": {"user_name": "test9"}}
I need to get "username",but i cant find the way to do it. Maybe i should change JSON to something like that:
[{"user_name": "test1"},{"user_name": "test5"},{"user_name": "test9"}]
This way i tried to get "user_name"s:
try {
List<String> allNames = new ArrayList<String>();
String str = "[{"user_name": "test1"},{"user_name": "test5"},{"user_name": "test9"}]";
JSONObject json = new JSONObject(str);
for ( int i = 0; i < json.length() ; i++)
{
JSONObject actor = json.getJSONObject(i);
String user_name = actor.getString("user_name");
allNames.add(user_name);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But then i get Problem with that line:
JSONObject actor = json.getJSONObject(i);
Android studio tells me that JSONObject cannot be applied to int(i). I have no idea why it doesn't work.
Can somebody please give me some hint, tutorial or an example?
Much appreciated !
The method JSONObject.getJSONObject requires a String argument (the name or key of the object), not an int. In your case, the keys are "17", "18" and "19". To get all keys, use the JSONObject.keys() method:
JSONObject json = new JSONObject(str);
Iterator<String> keyIterator = json.keys();
while (keyIterator.hasNext()) {
JSONObject actor = json.getJSONObject(keyIterator.next());
String user_name = actor.getString("user_name");
allNames.add(user_name);
}
Android studio tells me that JSONObject cannot be applied to int(i)
Because JSONObject.getJSONObject method takes key name as String instead of int. and keys in posted json is 17,18,... to access inner JSONObject.
To get all user_name from JSONObject,need to get all keys:
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
JSONObject actor = json.getJSONObject(key);
String user_name = actor.getString("user_name");
allNames.add(user_name);
}
Change your code like this
try {
List<String> allNames = new ArrayList<String>();
String str = "[{"user_name": "test1"},{"user_name": "test5"},{"user_name": "test9"}]";
JSONOArray jsonArray = new JSONOArray(str);
for ( int i = 0; i < jsonArray .length() ; i++)
{
JSONObject actor = jsonArray.getJSONObject(i);
String user_name = actor.getString("user_name");
allNames.add(user_name);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if you use the Gson library it's very easy.
String json = "{\"17\": {\"user_name\": \"test1\"}, \"18\": {\"user_name\": \"test5\"}, \"19\": {\"user_name\": \"test9\"}}";
Gson gson = new GsonBuilder().create();
Map<String,User> users = gson.fromJson(json, new TypeToken<Map<String,User> >(){}.getType());
for(String key : users.keySet()) {
Log.d("DEBUG",key+" -> "+users.get(key).getUser_name());
}
The User class is a simple POJO like:
public class User {
private String user_name;
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
}
str = {"17": {"user_name": "test1"}, "18": {"user_name": "test5"}, "19": {"user_name": "test9"}}
JSONObject ob1 = new JSONObject(str)
JSONObject ob2 = ob1.getJSONObject("17");
String userName = ob2.getString("user_name");
str = [{"user_name": "test1"},{"user_name": "test5"},{"user_name": "test9"}]
List<String> allNames = new ArrayList<String>();
JSONArray jsonArray = new JSONArray("str");
for(i=0; i < jsonArray.length(); i++){
JSONObject ob = jsonArray.getJSONObject(i);
allNames.add(ob.getString("user_name")
}
you are trying to parse JSONArray into JSONObject at
JSONObject json = new JSONObject(str);
use the below code.....
try {
List<String> allNames = new ArrayList<String>();
String str = "[{"user_name": "test1"},{"user_name": "test5"},{"user_name": "test9"}]";
JSONArray json = new JSONArray(str);
for ( int i = 0; i < json.length() ; i++)
{
JSONObject actor = json.getJSONObject(i);
String user_name = actor.getString("user_name");
allNames.add(user_name);
}
} catch (JSONException e) {
// TODO Auto-generated catch block`enter code here`
e.printStackTrace();
}

Categories

Resources