1

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.

6
  • what is JSON_MAP? it looks like you're probably trying to parse your JSON key, instead of the value (see the line Long time = Long.parseLong(dateKey); ) Commented Dec 9, 2013 at 22:36
  • it`s a hashMap, this is ho i declared them: static { JSON_MAP = new HashMap<Integer, String>(); JSON_MAP.put(MAP_OPEN, "Open"); //the rest JSON_MAP.put(MAP_DATE, "Time"); } Commented Dec 9, 2013 at 22:44
  • 1
    well, either way dateKey is always going to hold the value "Time" then, and I believe this is your problem, you're trying to parse "Time" as a Long :) Commented Dec 9, 2013 at 22:46
  • i guess i cannot format it in comments in a pretty way Commented Dec 9, 2013 at 22:46
  • @panini but it is Long, isn`t it? :) Commented Dec 9, 2013 at 22:47

2 Answers 2

2

the line Long time = Long.parseLong(dateKey);

is trying to parse the String Time as a Long. I think what you want to do is this

String timeString = array.getJSONObject(0).getString(dateKey);
Long time = Long.parseLong(timeString)

EDIT: re-reading your code, I think the issue is you are trying to parse your date too early.

you need to move the line Long time = Long.parseLong(dateKey); down into your JSON parsing loop.

then, instead of Long time = Long.parseLong(dateKey);, you should parse the date value as opposed to the date key, so Long.parseLong(date);

Sign up to request clarification or add additional context in comments.

3 Comments

it tells me to ad cast to 'array' , but then I`m not sure what do I put instead of 'Object' here: String timeString = ((Object) array).getObjectAt(0).getString(dateKey);
@Emily sorry, I had misread the question. I've updated my answer here, or you could check Adam S's answer which details a similar approach
i think now it takes all the date values in the document and sums them up, and then shows them. Because when i tried to uotprint date date then parsed date this is what i got: parsed date "Thu Jul 29 00:43:20 CEST 45909" and raw date: "333333333332111111111111386598949000"
2

You can create a new Date object, or a new DateTime object (from JodaTime - the recommended way to manipulate dates in Java) with a constructor that takes a long/Unix time in milliseconds:

long time;
// ...
Date date = new Date(time);
DateTime jodaDate = new DateTime(time);

Looking at your code, you can delete your parsing code entirely. Delete these lines:

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);

And replace these ones inside your loop for parsing the JSON 'Data' object:

String date = obj.getString(dateKey);
cp.dt = new Date(Long.parseLong(date));

result.add(cp);

Your "desired format" is the output format. You can format a date output string in this way:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //desired format
String formattedDate = df.format(date);

2 Comments

Thank you Adam for your response. I have to admit I was not able to implement what you said, however it looks very close to what I had from the very beginning. Thing is I`m not sure about DateTime jodaDate = new DateTime(time); do I have to create a class for that? and the "desired loop" code, do i put it in the for loop? Thanks
Yeah, at the end of your for loop, you have some similar code - you just replace it with the example stuff I provided. Don't worry about the DateTime object - it's part of the Joda Time library I linked to above. If all you're doing is parsing it from a Unix timestamp into a Date object and then displaying it, you don't need anything more. If you ever need to manipulate dates (adding days, comparing times between days, etc) then I'd recommend looking into the JodaTime library for assistance as it handles a lot of the problems you'll encounter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.