简体   繁体   中英

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.

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. 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.

I pass a string that holds the url to the server and use my HttpHandler to make the connection.

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:

 // 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. Look at your original structure. The actual path is $.bib.book , so to get that array, you need to do

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM