[英]Unable to properly parse json file
我正在開發一個隨機報價應用程序,我需要通過http連接來解析ajson文件。 我成功建立了連接; 並獲取json數據,但是當我嘗試解析它時,似乎我得到的鍵,值或對象未正確映射。 或者也許我沒有使用正確的代碼。 任何幫助表示贊賞。 謝謝
package com.example.george.radonquote;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Toolbar myToolbar;
String randQuote;
String author;
TextView quoteTextView;
ImageView bgImageView;
ImageView nextImageView;
String url = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=mycallback";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting the ActionBar for the activity
myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
quoteTextView = (TextView) findViewById(R.id.quote_text);
nextImageView = (ImageView) findViewById(R.id.next_quote);
/*onclick listener for next quote*/
nextImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FetchQuote fetchQuote = new FetchQuote();
fetchQuote.execute();
}
});
}
/*
* async class to get random quote in background
*/
class FetchQuote extends AsyncTask<Void, Void, Void> {
String quote = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"Fetching Quote!",Toast.LENGTH_LONG).show();
}
/*
* making connection and parsing json data
*/
@Override
protected Void doInBackground(Void... voids) {
try {
URL uri = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null) {
Log.v("line: ",line);
line = bf.readLine();
quote += line;
}
}
catch (MalformedURLException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
try {
JSONArray ja = new JSONArray(quote);
JSONObject jo = (JSONObject) ja.get(0);
randQuote = jo.get("content").toString().replaceAll("\\<[^>]*>","");
author = jo.get("title").toString();
Log.v("QUOTE", randQuote+" "+author);
}
catch (JSONException ex){
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// Log.v("Post Exec", randQuote);
quoteTextView.setText(randQuote);
}
}
}
您沒有從URL獲得有效的json。 首先糾正您的字符串,然后解析json。 嘗試下面的代碼。 這只是一個臨時解決方案。
try {
String requiredString = quote.substring(quote.indexOf("(") + 1, quote.indexOf(")"));
JSONArray ja = new JSONArray(requiredString );
JSONObject jo = (JSONObject) ja.get(0);
randQuote = jo.get("content").toString().replaceAll("\\<[^>]*>","");
author = jo.get("title").toString();
Log.v("QUOTE", randQuote+" "+author);
}
問題是網址中的_jsonp=mycallback
參數。 使用此參數會導致將格式錯誤的JSON返回給您。
如您在文檔中所見,JSONP參數在JavaScript中用於執行回調。
由於您在Android應用中使用Java,因此無需指定回調。
您需要做的就是修改url,以便它不定義回調:
String url = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
然后,響應將為有效格式的JSON,例如:
[
{
"ID":1572,
"title":"Si Scott",
"content":"<p>I really like looking at design and thinking: that attention to detail must have taken absolutely ages.<\/p>\n",
"link":"https:\/\/quotesondesign.com\/si-scott\/",
"custom_meta":{
"Source":"<a href=\"http:\/\/www.formatmag.com\/art\/si-scott\/\">article<\/a>"
}
}
]
然后,您現有的代碼應該可以正常工作。 我剛剛對其進行了測試,它記錄了正確的響應:
10-06 12:35:52.522 4606-4624/com.example.ex V/QUOTE: Graphic designers find themselves in a role of visual dishwashers for the Information Architects’ chefs.
Gunnar Swanson
使用JSOUP庫並遵循給定的代碼
String content;
content = Jsoup.parse(content).text();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.