gpt4 book ai didi

JavaScript 数组到 java

转载 作者:行者123 更新时间:2023-11-29 21:02:15 25 4
gpt4 key购买 nike

我有一个 javascript 数组代表我所有学校的时间表 https://dl.dropboxusercontent.com/u/16258361/urnik/data.js .我正在编写一个 android 应用程序,我需要下载数据并以某种方式将其解析为 java 数组。我什至可以使用 php 或其他网络服务来下载数据并制作 json/xml 或类似的东西。但到目前为止,我找不到任何好的解决方案,所以我向你询问任何想法。

谢谢:)

最佳答案

这应该有效。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class Main {
public static void main(String[] args) throws Exception {
String html = "";


URL url = new URL("https://dl.dropboxusercontent.com/u/16258361/urnik/data.js");
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
html += inputLine;
in.close();


String array[] = html.split("podatki");
ArrayList<String> dataArray = new ArrayList<String>();
for (int i = 2; i < array.length; i++)
dataArray.add(array[i]);

String last = dataArray.get(dataArray.size()-1);
String lastCleared = "";
for (int i = 0; i < last.length(); i++) {
if (!last.substring(i, i+1).contains("r")) lastCleared += last.substring(i, i+1);
else break;
}
dataArray.set(dataArray.size() - 1, lastCleared);

for (int i = 0; i < dataArray.size(); i++) {
String string = dataArray.get(i);
if (string.contains("new Array")) {
dataArray.remove(i);
i--;
}
else {
string = string.replaceAll("]\\[", ";");
string = string.replaceAll("\\[", "");
string = string.replaceAll("]", "");
string = string.replaceAll("\"", "");
string = string.replace(" = ", ";");
dataArray.set(i, string);
}
}

int lastIndex1;
String string = "";
String lastString = dataArray.get(dataArray.size() - 1);
for (int i = 0; i < lastString.length(); i++) {
if (lastString.substring(i, i+1).contains(";"))
break;
string += lastString.substring(i, i+1);
}

lastIndex1 = Integer.parseInt(string);
boolean start = false;
for (int i = 0; i < lastString.length(); i++) {
if (start) {
string = lastString.substring(i, i + 1);
break;
}
if (lastString.substring(i, i+1).contains(";"))
start = true;
}
int lastIndex2 = Integer.parseInt(string);

String finalData[][] = new String[lastIndex1+1][lastIndex2+1];
for (int i = 0; i < dataArray.size(); i++) {
String components[] = dataArray.get(i).split(";");
if (components.length == 3)
finalData[Integer.parseInt(components[0])][Integer.parseInt(components[1])] = components[2];
else
finalData[Integer.parseInt(components[0])][Integer.parseInt(components[1])] = "";
}

for (int i = 0; i < finalData.length; i++) {
for (int j = 0; j < finalData[i].length; j++)
System.out.println(finalData[i][j]);
System.out.println("\n");
}
}
}

关于JavaScript 数组到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25704694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com