change contents json parser

This commit is contained in:
Chumva 2018-04-13 16:01:08 +03:00
parent 70e23931de
commit 82f0a9ccd8
3 changed files with 161 additions and 11 deletions

View file

@ -57,6 +57,11 @@ public class WikivoyageArticleContentsFragment extends MenuBottomSheetDialogFrag
return;
}
ContentsJsonParser.Container contentsContainer2 = ContentsJsonParser.parseJsonContents2(contentsJson);
if (contentsContainer2 == null) {
return;
}
final ArrayList<String> listDataHeader = contentsContainer.listDataHeader;
final LinkedHashMap<String, List<String>> listDataChild = contentsContainer.listDataChild;

View file

@ -50,6 +50,21 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
"window.location.hash = id;}</script>";
private static final String FOOTER_INNER = "</div></body></html>";
private static final String collapsibleJS = "<script>\n" +
"var coll = document.getElementsByTagName(\"H2\");\n" +
"var i;\n" +
"for (i = 0; i < coll.length; i++) {\n" +
" coll[i].addEventListener(\"click\", function() {\n" +
" this.classList.toggle(\"active\");\n" +
" var content = this.nextElementSibling;\n" +
" if (content.style.display === \"block\") {\n" +
" content.style.display = \"none\";\n" +
" } else {\n" +
" content.style.display = \"block\";\n" +
" }\n" +
" });\n" +
"}</script>";
private long cityId = NO_VALUE;
private ArrayList<String> langs;
private String selectedLang;

View file

@ -17,6 +17,7 @@ public class ContentsJsonParser {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
ArrayList<String> listDataHeader = new ArrayList<>();
LinkedHashMap<String, List<String>> listDataChild = new LinkedHashMap<>();
Container headers = new Container();
JSONObject reader;
try {
@ -26,26 +27,49 @@ public class ContentsJsonParser {
return null;
}
List<String> secondLevel = null;
JSONArray jArray = reader.names();
JSONObject jArray = null;
try {
jArray = reader.getJSONObject("headers");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jArray.length(); i++) {
try {
JSONArray contacts = reader.getJSONArray(reader.names().getString(i));
String link = contacts.getString(1);
map.put(reader.names().getString(i), link);
String link = "";
JSONObject header = jArray.getJSONObject(jArray.names().getString(i));
link = header.getString("link");
map.put(jArray.names().getString(i), link);
int level = contacts.getInt(0);
if (level == 2) {
listDataHeader.add(reader.names().getString(i));
Container firstLvl = new Container(jArray.names().getString(i), link);
headers.childs.add(firstLvl);
firstLvl.setParent(headers);
listDataHeader.add(jArray.names().getString(i));
JSONArray contacts = header.getJSONArray("subheaders");
if (contacts.length() > 1) {
secondLevel = new ArrayList<>();
}
if (level == 3) {
for (int j = 0; j < contacts.length(); j++) {
String names = header.names().toString();
JSONObject subheader = contacts.getJSONObject(j);
JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0));
if (secondLevel == null) {
secondLevel = new ArrayList<>();
}
secondLevel.add(reader.names().getString(i));
secondLevel.add(subheader.names().getString(0));
listDataChild.put(listDataHeader.get(listDataHeader.size() - 1), secondLevel);
link = subheader2.getString("link");
map.put(subheader.names().getString(0), link);
Container secondLvl = new Container(subheader.names().getString(0), link);
firstLvl.childs.add(secondLvl);
secondLvl.setParent(headers);
}
} catch (JSONException e) {
e.printStackTrace();
@ -54,6 +78,64 @@ public class ContentsJsonParser {
return new ContentsContainer(map, listDataHeader, listDataChild);
}
@Nullable
public static Container parseJsonContents2(String contentsJson) {
Container headers = new Container();
JSONObject reader;
try {
reader = new JSONObject(contentsJson);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
List<String> secondLevel = null;
JSONObject jArray = null;
try {
jArray = reader.getJSONObject("headers");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jArray.length(); i++) {
try {
String link = "";
JSONObject header = jArray.getJSONObject(jArray.names().getString(i));
link = header.getString("link");
Container firstLvl = new Container(jArray.names().getString(i), link);
headers.childs.add(firstLvl);
firstLvl.setParent(headers);
JSONArray contacts = header.getJSONArray("subheaders");
if (contacts.length() > 1) {
secondLevel = new ArrayList<>();
}
for (int j = 0; j < contacts.length(); j++) {
JSONObject subheader = contacts.getJSONObject(j);
JSONObject subheader2 = subheader.getJSONObject(subheader.names().getString(0));
if (secondLevel == null) {
secondLevel = new ArrayList<>();
}
secondLevel.add(subheader.names().getString(0));
link = subheader2.getString("link");
Container secondLvl = new Container(subheader.names().getString(0), link);
firstLvl.childs.add(secondLvl);
secondLvl.setParent(headers);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return headers;
}
public static class ContentsContainer {
public LinkedHashMap<String, String> map;
@ -61,11 +143,59 @@ public class ContentsJsonParser {
public LinkedHashMap<String, List<String>> listDataChild;
ContentsContainer(LinkedHashMap<String, String> map,
ArrayList<String> listDataHeader,
LinkedHashMap<String, List<String>> listChildData) {
ArrayList<String> listDataHeader,
LinkedHashMap<String, List<String>> listChildData) {
this.map = map;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
}
public static class Container {
String name;
String link;
ArrayList<Container> childs = new ArrayList<>();
Container parent;
public ArrayList<Container> getChilds() {
return childs;
}
public void setChilds(ArrayList<Container> childs) {
this.childs = childs;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Container getParent() {
return parent;
}
public void setParent(Container parent) {
this.parent = parent;
}
public Container(String name, String link) {
this.name = name;
this.link = link;
}
public Container() {
}
}
}