我想解析Android Studio中的JSON脚本,我必须获取JSON脚本,这是HTML文档的一部分,而不是HTML部分。是HTML文档中的其他JSON脚本,但我必须专门使用此部分/脚本以下是我必须使用的HTML文档的示例... (HTML文档是我不拥有的网页的源代码)
...........other HTML parts
</script><script type="application/ld+json">
[ ...........start of the JSON part
{
"@context":"http://schema.org",
"@type":"Event",
... ...........other JSON data
"name": "EasyLearning Tre"
}
]
}
] ...........end of the JSON part
</script><div class="eh-carousel-header row">
...........other HTML parts
我已经使用ASyncTask下载了整个HTML文档,现在我想获得本文档的JSON部分。这是我尝试过的......
public class MainActivity extends AppCompatActivity {
EditText urlContents;
String content=null;
Button navigate;
StringBuilder sb;
public void navigate(View v){
DownloadContents downloadContents = new DownloadContents();
try{
navigate.setText("Downloading...");content=downloadContents.execute("https://www.eventshigh.com/bangalore/technology?src=home").get();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
public class DownloadContents extends AsyncTask<String,Void ,String> {
String result="";
@Override
protected String doInBackground(String... urls) {
try {
sb = new StringBuilder();
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.connect();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
String line = null;
while((line = reader.readLine())!=null){
sb.append(line + "\n");
}
result = sb.toString();
in.close();
return result;
} catch (Exception e) {
return e.toString();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
navigate.setText("Downloaded...");
String[] splitResult= result.split("</script><script type=\"application/ld+json\">");
String[] nextSplitResult=splitResult[1].split("<script><div class=\"eh-carousel-header row\">");
String mainResult=nextSplitResult[0];
urlContents.setText(mainResult);
}
}
}
但我发现,拆分功能并没有拆分文件。
非常感谢任何帮助。