Good day. I'm working on little application and currently I'm trying to parse a Unix time from JSON object (on CouchDB). I have tried several ways, but this one I think is closest. Now I get NumberFormatException: Invalid long: "Time" error
private Points parseJsonDocument(JSONObject json) throws JSONException, ParseException
{
String openKey = JSON_MAP.get(MAP_OPEN);
String closeKey = JSON_MAP.get(MAP_CLOSE);
String highKey = JSON_MAP.get(MAP_HIGH);
String lowKey = JSON_MAP.get(MAP_LOW);
String volumeKey = JSON_MAP.get(MAP_VOLUME);
String dateKey = JSON_MAP.get(MAP_DATE);
JSONObject object = json.getJSONObject("Stock");
JSONArray array = object.getJSONArray("Data");
// JSONArray array = json.getJSONArray("Data");
Points result = new Points();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //desired format
Long time = Long.parseLong(dateKey);
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time *1000);
for(int i=0;i<array.length();i++)
{
JSONObject obj = array.getJSONObject(i);
Point cp = new Point();
cp.c = obj.getDouble(closeKey);
cp.h = obj.getDouble(highKey);
cp.l = obj.getDouble(lowKey);
cp.o = obj.getDouble(openKey);
cp.v = obj.getDouble(volumeKey);
String date = obj.getString(dateKey);
cp.dt = df.parse(df.format(cal.getTime()));
result.add(cp);
}
return result;
}
and JSON object looks like this:
{
"_id": "abbv",
"_rev": "12-3d250de8395a12e0c92dfb9d7d379fc4",
"Stock": {
"Data": [
{
"Time": "1386598949000",
"Open": "50.17",
"High": "0",
"Low": "0",
"Close": "51.37",
"Volume": "7285755"
}
]
}
It's probably some silly mistake which I just don't see because of lack of experience, so any advice or guidance will be appreciated. And yes,I have tried reading javadocs but I could't find the solution. Thanks.