JAVA JSON identical keys: how to search? [on hold]











up vote
-2
down vote

favorite












I am getting a JSON response from an API, that has the following format:



{  
"timestamp":1542790613,
"price":4608.4564197580685
},
{
"timestamp":1542790614,
"price":4608.456348092037
},
{
"timestamp":1542790615,
"price":4608.358785472172
},
{
"timestamp":1542790616,
"price":4608.420295035769
},
{
"timestamp":1542790617,
"price":4608.420058274378
}


Now, I've converted into a string (can't paste the code here, company policy), but the timestamp shows like 1.54278925E9 instead of showing all the numbers.



What I'm trying to do is, get a timestamp (unixtime) parameter from somewhere else, search it in the JSON and return the price for that timestamp.



I'm rather new to Java, so, please, have mercy!










share|improve this question















put on hold as off-topic by Seelenvirtuose, Andrea, Werner Henze, Mickael Maison, EdChum yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andrea, Werner Henze, Mickael Maison, EdChum

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
    – Ivo Vidovic
    yesterday












  • how are you converting it to string?
    – Nawnit Sen
    yesterday










  • I don't get it. You have string with JSON and convert it to string. Why?
    – talex
    yesterday










  • These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
    – Ionut Apostu
    yesterday






  • 1




    Title doesn't seem to reflect the question - more about type conversion than search.
    – JonathanS
    yesterday















up vote
-2
down vote

favorite












I am getting a JSON response from an API, that has the following format:



{  
"timestamp":1542790613,
"price":4608.4564197580685
},
{
"timestamp":1542790614,
"price":4608.456348092037
},
{
"timestamp":1542790615,
"price":4608.358785472172
},
{
"timestamp":1542790616,
"price":4608.420295035769
},
{
"timestamp":1542790617,
"price":4608.420058274378
}


Now, I've converted into a string (can't paste the code here, company policy), but the timestamp shows like 1.54278925E9 instead of showing all the numbers.



What I'm trying to do is, get a timestamp (unixtime) parameter from somewhere else, search it in the JSON and return the price for that timestamp.



I'm rather new to Java, so, please, have mercy!










share|improve this question















put on hold as off-topic by Seelenvirtuose, Andrea, Werner Henze, Mickael Maison, EdChum yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andrea, Werner Henze, Mickael Maison, EdChum

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
    – Ivo Vidovic
    yesterday












  • how are you converting it to string?
    – Nawnit Sen
    yesterday










  • I don't get it. You have string with JSON and convert it to string. Why?
    – talex
    yesterday










  • These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
    – Ionut Apostu
    yesterday






  • 1




    Title doesn't seem to reflect the question - more about type conversion than search.
    – JonathanS
    yesterday













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am getting a JSON response from an API, that has the following format:



{  
"timestamp":1542790613,
"price":4608.4564197580685
},
{
"timestamp":1542790614,
"price":4608.456348092037
},
{
"timestamp":1542790615,
"price":4608.358785472172
},
{
"timestamp":1542790616,
"price":4608.420295035769
},
{
"timestamp":1542790617,
"price":4608.420058274378
}


Now, I've converted into a string (can't paste the code here, company policy), but the timestamp shows like 1.54278925E9 instead of showing all the numbers.



What I'm trying to do is, get a timestamp (unixtime) parameter from somewhere else, search it in the JSON and return the price for that timestamp.



I'm rather new to Java, so, please, have mercy!










share|improve this question















I am getting a JSON response from an API, that has the following format:



{  
"timestamp":1542790613,
"price":4608.4564197580685
},
{
"timestamp":1542790614,
"price":4608.456348092037
},
{
"timestamp":1542790615,
"price":4608.358785472172
},
{
"timestamp":1542790616,
"price":4608.420295035769
},
{
"timestamp":1542790617,
"price":4608.420058274378
}


Now, I've converted into a string (can't paste the code here, company policy), but the timestamp shows like 1.54278925E9 instead of showing all the numbers.



What I'm trying to do is, get a timestamp (unixtime) parameter from somewhere else, search it in the JSON and return the price for that timestamp.



I'm rather new to Java, so, please, have mercy!







java json search indexing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Nesku

14819




14819










asked yesterday









Ionut Apostu

1




1




put on hold as off-topic by Seelenvirtuose, Andrea, Werner Henze, Mickael Maison, EdChum yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andrea, Werner Henze, Mickael Maison, EdChum

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Seelenvirtuose, Andrea, Werner Henze, Mickael Maison, EdChum yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andrea, Werner Henze, Mickael Maison, EdChum

If this question can be reworded to fit the rules in the help center, please edit the question.












  • Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
    – Ivo Vidovic
    yesterday












  • how are you converting it to string?
    – Nawnit Sen
    yesterday










  • I don't get it. You have string with JSON and convert it to string. Why?
    – talex
    yesterday










  • These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
    – Ionut Apostu
    yesterday






  • 1




    Title doesn't seem to reflect the question - more about type conversion than search.
    – JonathanS
    yesterday


















  • Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
    – Ivo Vidovic
    yesterday












  • how are you converting it to string?
    – Nawnit Sen
    yesterday










  • I don't get it. You have string with JSON and convert it to string. Why?
    – talex
    yesterday










  • These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
    – Ionut Apostu
    yesterday






  • 1




    Title doesn't seem to reflect the question - more about type conversion than search.
    – JonathanS
    yesterday
















Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
– Ivo Vidovic
yesterday






Just for clarification: these objects are in an array/list right? If so, please edit the question to add the [ ] brackets
– Ivo Vidovic
yesterday














how are you converting it to string?
– Nawnit Sen
yesterday




how are you converting it to string?
– Nawnit Sen
yesterday












I don't get it. You have string with JSON and convert it to string. Why?
– talex
yesterday




I don't get it. You have string with JSON and convert it to string. Why?
– talex
yesterday












These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
– Ionut Apostu
yesterday




These objects come under this form: [{ "timestamp": 1542790613, "price": 4608.4564197580685 }, { "timestamp": 1542790614, "price": 4608.456348092037 }, { "timestamp": 1542790615, "price": 4608.358785472172 }, { "timestamp": 1542790616, "price": 4608.420295035769 }, { "timestamp": 1542790617, "price": 4608.420058274378 }]
– Ionut Apostu
yesterday




1




1




Title doesn't seem to reflect the question - more about type conversion than search.
– JonathanS
yesterday




Title doesn't seem to reflect the question - more about type conversion than search.
– JonathanS
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote













The timestamp values in the json are milliseconds since the epoch. It looks like you are reading this in and ending up with a long, then converting to a string whereby you get the exponential expression of this number. Skip the string conversion, create an Instant object using the long value. The Instant class knows how to handle this.



Instant timestamp = Instant.ofEpochMilli(timestampValueFromJson);


See https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html



Update: If you are using org.json to parse, then call jsonobj.getLong("timestamp") rather than the implicit jsonobj.get("timestamp"). The latter will return a number object so the string conversion is unnecessary.






share|improve this answer























  • Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
    – Ionut Apostu
    yesterday






  • 2




    Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
    – MC Emperor
    yesterday










  • Good point, answered edited
    – JonathanS
    yesterday










  • @JonathanS That's better :-)
    – MC Emperor
    yesterday


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













The timestamp values in the json are milliseconds since the epoch. It looks like you are reading this in and ending up with a long, then converting to a string whereby you get the exponential expression of this number. Skip the string conversion, create an Instant object using the long value. The Instant class knows how to handle this.



Instant timestamp = Instant.ofEpochMilli(timestampValueFromJson);


See https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html



Update: If you are using org.json to parse, then call jsonobj.getLong("timestamp") rather than the implicit jsonobj.get("timestamp"). The latter will return a number object so the string conversion is unnecessary.






share|improve this answer























  • Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
    – Ionut Apostu
    yesterday






  • 2




    Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
    – MC Emperor
    yesterday










  • Good point, answered edited
    – JonathanS
    yesterday










  • @JonathanS That's better :-)
    – MC Emperor
    yesterday















up vote
1
down vote













The timestamp values in the json are milliseconds since the epoch. It looks like you are reading this in and ending up with a long, then converting to a string whereby you get the exponential expression of this number. Skip the string conversion, create an Instant object using the long value. The Instant class knows how to handle this.



Instant timestamp = Instant.ofEpochMilli(timestampValueFromJson);


See https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html



Update: If you are using org.json to parse, then call jsonobj.getLong("timestamp") rather than the implicit jsonobj.get("timestamp"). The latter will return a number object so the string conversion is unnecessary.






share|improve this answer























  • Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
    – Ionut Apostu
    yesterday






  • 2




    Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
    – MC Emperor
    yesterday










  • Good point, answered edited
    – JonathanS
    yesterday










  • @JonathanS That's better :-)
    – MC Emperor
    yesterday













up vote
1
down vote










up vote
1
down vote









The timestamp values in the json are milliseconds since the epoch. It looks like you are reading this in and ending up with a long, then converting to a string whereby you get the exponential expression of this number. Skip the string conversion, create an Instant object using the long value. The Instant class knows how to handle this.



Instant timestamp = Instant.ofEpochMilli(timestampValueFromJson);


See https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html



Update: If you are using org.json to parse, then call jsonobj.getLong("timestamp") rather than the implicit jsonobj.get("timestamp"). The latter will return a number object so the string conversion is unnecessary.






share|improve this answer














The timestamp values in the json are milliseconds since the epoch. It looks like you are reading this in and ending up with a long, then converting to a string whereby you get the exponential expression of this number. Skip the string conversion, create an Instant object using the long value. The Instant class knows how to handle this.



Instant timestamp = Instant.ofEpochMilli(timestampValueFromJson);


See https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html



Update: If you are using org.json to parse, then call jsonobj.getLong("timestamp") rather than the implicit jsonobj.get("timestamp"). The latter will return a number object so the string conversion is unnecessary.







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday









MC Emperor

7,734125387




7,734125387










answered yesterday









JonathanS

1838




1838












  • Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
    – Ionut Apostu
    yesterday






  • 2




    Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
    – MC Emperor
    yesterday










  • Good point, answered edited
    – JonathanS
    yesterday










  • @JonathanS That's better :-)
    – MC Emperor
    yesterday


















  • Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
    – Ionut Apostu
    yesterday






  • 2




    Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
    – MC Emperor
    yesterday










  • Good point, answered edited
    – JonathanS
    yesterday










  • @JonathanS That's better :-)
    – MC Emperor
    yesterday
















Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
– Ionut Apostu
yesterday




Thank you for the suggestion, but I have to find a link between time and price and compare the time, then display the price for that time. So, maybe two arrays would be the answer. Do you mind explaining your answer with some code? How do I get all 59 timestamp values into an array?
– Ionut Apostu
yesterday




2




2




Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
– MC Emperor
yesterday




Don't use the obsolete date and time classes, like Date and Calendar. Use the new Java Date and Time API available in the java.time package.
– MC Emperor
yesterday












Good point, answered edited
– JonathanS
yesterday




Good point, answered edited
– JonathanS
yesterday












@JonathanS That's better :-)
– MC Emperor
yesterday




@JonathanS That's better :-)
– MC Emperor
yesterday



Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)