Skip to content

Instantly share code, notes, and snippets.

@masudias
Created March 7, 2019 00:53
Show Gist options
  • Save masudias/f193720244f775f8d086a6d827178f03 to your computer and use it in GitHub Desktop.
Save masudias/f193720244f775f8d086a6d827178f03 to your computer and use it in GitHub Desktop.
public class History {
public String date;
public HistoryElements elements;
}
public class HistoryElements {
public String volume;
public String high;
public String low;
public String close;
public String open;
}
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;
}
}
import java.util.ArrayList;
public class Repo {
public String name;
public ArrayList<History> histories;
public Repo() {
histories = new ArrayList<History>();
}
}
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;
}
}
@masudias
Copy link
Author

masudias commented Mar 7, 2019

As an answer to this question in StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment