Created
March 7, 2019 00:53
-
-
Save masudias/f193720244f775f8d086a6d827178f03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class History { | |
public String date; | |
public HistoryElements elements; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HistoryElements { | |
public String volume; | |
public String high; | |
public String low; | |
public String close; | |
public String open; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public class Main { | |
public static final String jsonString = "{\n" + | |
" \"name\": \"AAPL\",\n" + | |
" \"history\": {\n" + | |
" \"2019-03-05\": {\n" + | |
" \"open\": \"175.94\",\n" + | |
" \"close\": \"175.53\",\n" + | |
" \"high\": \"176.00\",\n" + | |
" \"low\": \"174.54\",\n" + | |
" \"volume\": \"19163899\"\n" + | |
" },\n" + | |
" \"2019-03-04\": {\n" + | |
" \"open\": \"175.69\",\n" + | |
" \"close\": \"175.85\",\n" + | |
" \"high\": \"177.75\",\n" + | |
" \"low\": \"173.97\",\n" + | |
" \"volume\": \"27436203\"\n" + | |
" }\n" + | |
" }\n" + | |
"} "; | |
public static void main(String[] args) { | |
try { | |
Repo repo = RepoParser.parseRepo(jsonString); | |
System.out.println(repo.name); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Repo { | |
public String name; | |
public ArrayList<History> histories; | |
public Repo() { | |
histories = new ArrayList<History>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.oracle.javafx.jmx.json.JSONException; | |
import org.json.JSONObject; | |
import java.text.ParseException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
public class RepoParser { | |
public static Repo parseRepo(String jsonString) throws ParseException, JSONException { | |
JSONObject jsonObject = new JSONObject(jsonString); | |
Iterator<?> iterator = jsonObject.keys(); | |
Repo repo = new Repo(); | |
while (iterator.hasNext()) { | |
String obj = iterator.next().toString(); | |
if (obj.equals("name")) repo.name = obj; | |
else repo.histories = parseHistory((JSONObject) jsonObject.get(obj)); | |
} | |
return repo; | |
} | |
public static ArrayList<History> parseHistory(JSONObject jsonObject) throws ParseException, JSONException { | |
Iterator<?> iterator = jsonObject.keys(); | |
ArrayList<History> historyList = new ArrayList<>(); | |
while (iterator.hasNext()) { | |
String obj = iterator.next().toString(); | |
History history = new History(); | |
history.date = obj; | |
history.elements = parseHistoryElement((JSONObject) jsonObject.get(obj)); | |
historyList.add(history); | |
} | |
return historyList; | |
} | |
public static HistoryElements parseHistoryElement(JSONObject jsonObject) throws ParseException, JSONException { | |
Iterator<?> iterator = jsonObject.keys(); | |
HistoryElements historyElements = new HistoryElements(); | |
while (iterator.hasNext()) { | |
String obj = iterator.next().toString(); | |
if (obj.equals("open")) historyElements.open = jsonObject.getString("open"); | |
if (obj.equals("close")) historyElements.close = jsonObject.getString("close"); | |
if (obj.equals("high")) historyElements.high = jsonObject.getString("high"); | |
if (obj.equals("low")) historyElements.low = jsonObject.getString("low"); | |
if (obj.equals("volume")) historyElements.volume = jsonObject.getString("volume"); | |
} | |
return historyElements; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As an answer to this question in StackOverflow.