powernit powernit - 3 months ago 11
Java Question

professor's research student gave JSON data which is not structured correctly as a JSON array of users

Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1431)
at java.util.HashMap$KeyIterator.next(HashMap.java:1453)
at DataParse.parseFeed(DataParse.java:61)
at intellidealsbackend.main(intellibackend.java:25)


Here is how the JSONObject looks which I am trying to parse:

{
users: {
276c7438-2b6a-4d92-a5f8-4a9e9007efa8: {
email: "power.nit@gmail.com",
username: "power nit"
},

de977b2f-2243-425c-9dfe-9f1443e41303: {
email: "denvervronkos@gmail.com",
username: "Denver vronkos"
},
e73cd6c9-9577-4cdc-b8fe-4227ac227c6b: {
email: "ls@gmail.com",
username: "laveena sharma"
}
},
visited: {
2fe51b4a-60b9-42cf-a889-bb607a5ea9bd: {
-KGWYAYM_iW6vi_SGBq3: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.7932608",
longitude: "-118.1359034",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "03:24",
uid: "3b0b90ce-8d13-4c7b-ab5b-b453aa9fabc5",
username: "kunal bolia"
},
-KGWYK9wH7UPcXMNCisY: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.79330768",
longitude: "-118.13585834",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "03:25",
uid: "3b0b90ce-8d13-4c7b-ab5b-b453aa9fabc5",
username: "kunal bolia"
}
},
68ebe7a6-7344-4d50-b647-16bb830ad413: {
-KGWB4NOqJGg6DNSPN1n: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.7932834",
longitude: "-118.135812",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "01:43",
uid: "68ebe7a6-7344-4d50-b647-16bb830ad413",
username: "bat man"
},
-KGWBIvlt9pUQOrJgK0p: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.79357387",
longitude: "-118.13580848",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "01:44",
uid: "68ebe7a6-7344-4d50-b647-16bb830ad413",
username: "bat man"
},
-KGWQi36kjznhtMerapt: {
condition: "clear sky",
date: "29 Apr 2016",
degree: "59.90360000000002",
latitude: "33.7932608",
longitude: "-118.1359034",
placeType: "Shopping Mall",
placeVisited: "Los Altos Mall",
time: "02:52",
uid: "68ebe7a6-7344-4d50-b647-16bb830ad413",
username: "bat man"
}
}


This is my implementation of the parser:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class DataParse {


private static List<String> uids;


public static List<DataModal> parseFeed(String content) {
try {

List<DataModal> dataList = new ArrayList<>();
uids = new ArrayList<>();

//get root node
JSONObject data = new JSONObject(content);
if(data.has("users")&& data.has("visited")&& !data.isNull("users")&& !data.isNull("visited")){
// We get parent nodes
JSONObject userObj = data.getJSONObject("users");
JSONObject visitedObj = data.getJSONObject("visited");

//get map from JSON object for its keys
JSONToMap jtm = new JSONToMap();
Map<String,Object> userId = jtm.jsonToMap(userObj);

//these are the keys from all the users
Set<String> userKeys = userId.keySet();

uids.addAll(userKeys);

for(Iterator i = userKeys.iterator();i.hasNext();){

//boolean test = visitedObj.isNull(i.next().toString());

if(visitedObj.isNull(i.next().toString())||!visitedObj.has(i.next().toString())){

continue;
}
else{

JSONObject visitedDatauid = visitedObj.getJSONObject(i.next().toString());
//get map from JSON object for its keys

JSONToMap jtmPushKeys = new JSONToMap();
Map<String,Object> pushKeys = jtmPushKeys.jsonToMap(visitedDatauid);
Set<String> pKeys = pushKeys.keySet();
//iterate through uid -> unique keys from push in android
for(Iterator j = pKeys.iterator();j.hasNext();)
{

//access push key 'j' data for particular uid 'i'
if(!(visitedDatauid.isNull(j.next().toString()))&& (visitedDatauid.has(j.next().toString()))){

JSONObject visitdata = visitedDatauid.getJSONObject(j.next().toString());

//String uidIter = i.next().toString();


//JSONObject user = userObj.getJSONObject(i.next().toString());

//Get data from JSONObjects and store in Strings
String uid="",username="",email="",placevisited="",lat="",lon="", condition ="";

//String uid="",username="",email="",placevisited="",lat="",lon="", condition ="";

uid = visitdata.getString("uid");
username = visitdata.getString("username");
//email = user.getString("email");
placevisited = visitdata.getString("placeVisited");
lat = visitdata.getString("latitude");
lon = visitdata.getString("longitude");
condition = visitdata.getString("condition");

//DataModal dm = new DataModal(email,uid,username,lat,lon,placevisited,condition);
DataModal dm = new DataModal(uid,username,lat,lon,placevisited,condition);
dataList.add(dm);

}

else{

System.err.println("Visited key:"+j.next().toString()+" do not exist in Visited");

String uid="",username="",email="",placevisited="",lat="",lon="", condition ="";

//String uid="",username="",email="",placevisited="",lat="",lon="", condition ="";

uid = i.next().toString();
username = "NA";
email = "NA";
placevisited = "NA";
lat = "NA";
lon = "NA";
condition = "NA";

DataModal dm = new DataModal(uid,username,lat,lon,placevisited,condition);
dataList.add(dm);
continue;
}

}

}
}
}


return dataList;
} catch (JSONException e) {
e.printStackTrace();
return null;
}

}


}

I have been trying to debug it. When the iterator hits a null or a value not present in visited node it throws NoSuchElementException even though I have checking condition in place the code still throws this error.

I have searched through the internet and only checks I found are with json.has(key) and json.isNull(key) which I am already performing.How can I resolve this issue.

Thanks I really appreciate your help.

Answer

As has been stated, your JSON is invalid. Both sides need to be in quotes and you need to put arrays of information in []:

For example:

{
"users": [{
    "276c7438-2b6a-4d92-a5f8-4a9e9007efa8": {
        "email": "power.nit@gmail.com",
        "username": "power nit"
    },
    "de977b2f-2243-425c-9dfe-9f1443e41303": {
        "email": "denvervronkos@gmail.com",
        "username": "Denver vronkos"
    },
    "e73cd6c9-9577-4cdc-b8fe-4227ac227c6b": {
        "email": "ls@gmail.com",
        "username": "laveena sharma"
    }
}]
}
Comments