[英]Parsing json from a file on a server
I am working on an android project that parses JSON from a file on a server and converting the data into java objects to display the data using text views.我正在开发一个 android 项目,该项目从服务器上的文件解析 JSON,并将数据转换为 java 对象以使用文本视图显示数据。
I have created a HTTP connection so that the app can make a GET request to the server to retrieve the contents of the file.我创建了一个 HTTP 连接,以便应用程序可以向服务器发出 GET 请求以检索文件的内容。 I also have a method to convert JSON to string and display the JSON in the logcat, so that I know the GET request was successful.
我还有一个方法可以将 JSON 转换为字符串并在 logcat 中显示 JSON,这样我就知道 GET 请求成功了。
I pass a string that holds the url to the server and use my HttpHandler to make the connection.我将一个包含 url 的字符串传递给服务器并使用我的 HttpHandler 建立连接。
private class parseJSON extends AsyncTask<Void, Void, List<Book>> {
private final String TAG = parseJSON.class.getSimpleName();
@Override
protected List<Book> doInBackground(Void... voids) {
Log.i(TAG, "Start Async to get books.");
ArrayList<Book> bookArray = new ArrayList<>(0);
String jsonUrl = getApplication().getString(R.string.json_feed);
HttpHandler httpHandler = new HttpHandler();
String jsonString = httpHandler.makeJsonServiceCall(jsonUrl);
Log.i(TAG, "Response from url: " + jsonString);
if( jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
// Get JSON array node.
JSONArray jArray = jsonObject.getJSONArray("book");
// Looping through all the books.
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonEntry = jArray.getJSONObject(i);
String year = jsonEntry.getString("year");
String title = jsonEntry.getString("title");
String publisher = jsonEntry.getString("publisher");
String price = "£" + jsonEntry.getString("price");
final Book bookObject = new Book(year, title, publisher, price);
//Add the new books to our result array.
bookArray.add(bookObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return bookArray;
}
@Override
protected void onPostExecute( List<Book> books) {
super.onPostExecute(books);
Log.e(TAG, "Populate UI recycler view with json converted data.");
// bookArray
bookList.setValue(books);
}
}
The parsing seems to get interrupted when I try to get the array node.当我尝试获取数组节点时,解析似乎被中断了。
The following issues arise:出现以下问题:
org.json.JSONException: Value "Contents of my json file"
at org.json.JSON.typeMismatch(JSON.java:101)
at org.json.JSONObject.getJSONArray(JSONObject.java:591)
Then refers to a line 170 which is:然后引用第 170 行,它是:
// Get JSON array node.
JSONArray jArray = jsonObject.getJSONArray("book");
Could I be doing something wrong with the parsing process?我会不会在解析过程中做错了什么?
Nothing wrong with your parsing, but your JSONPath is bad.您的解析没有问题,但是您的 JSONPath 很糟糕。 Look at your original structure.
看看你的原始结构。 The actual path is
$.bib.book
, so to get that array, you need to do实际路径是
$.bib.book
,因此要获取该数组,您需要执行
new JSONObject(jsonString).getJSONObject("bib").getJSONArray("book")
You may be able to do .getJSONArray("bib.book")
but I'm not sure how that class parses JSON and if nested paths are supported.您也许可以执行
.getJSONArray("bib.book")
但我不确定该类如何解析 JSON 以及是否支持嵌套路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.