0

I am studying. Please do not kick me ))) I was reading how to parse JSON from array to strings, but my document starts with 2 attributes and only then goes into an array.

"total": 56964,
"last": "2016-05-03 09:12:00",
"quotes": [
    {
        "id": 439124,
        "description": "Xxx: А давай какую-нибудь фирму заведем.<br>Yyy: o.O Зачем?<br>Xxx: Ну, у меня же аллергия. Кошек нельзя :)",
        "time": "2016-05-03 09:12:00",
        "rating": 0
    }

Here's my array "quotes". How i must to handle two items before array?

And last. Array has 50 items. Best way to show this on screen is use ListView with 2 textview for "total" and "last" + textviews for each item in array?

9
  • This is not valid JSON - refer this Commented May 3, 2016 at 20:08
  • Valid JSON would look something like: { "total": 56964, "last": "2016-05-0309: 12: 00", "quotes": [ { "id": 439124, "description": "Xxx: Адавайкакую-нибудьфирмузаведем.<br>Yyy: o.OЗачем?<br>Xxx: Ну,уменяжеаллергия.Кошекнельзя: )", "time": "2016-05-0309: 12: 00", "rating": 0 } ] } Commented May 3, 2016 at 20:09
  • Well, i can download my target json. Would it help? Commented May 3, 2016 at 20:12
  • To parse JSON you require valid JSON. You cannot parse a random string as JSON. Commented May 3, 2016 at 20:13
  • I cant understand. I have link from my client. But i cant put it here. What can i do to receive help to me any way? I put here text only for demonstration of structure. I did not right? Commented May 3, 2016 at 20:15

2 Answers 2

2

Try this:

               try {
                    String json = ""; // This should be the JSON from Your API
                    JSONObject jObj = new JSONObject(json);
                    String total = jObj.optString("total");
                    String last = jObj.optString("last");
                    JSONArray jsonArray = jObj.getJSONArray("quotes");

                } catch (JSONException jex) {
                    jex.printStackTrace();
                }

jsonArray now holds the quotes array, use a for loop and extract the nested JSONObjects one by one.

total and last hold the two variables before your quotes array.

As a best practise you can define your JSON nodes as private static Strings just after your class declaration:

private static String TOTAL = "total"
Sign up to request clarification or add additional context in comments.

4 Comments

"total" should be an int or a double
Can you support me with displaying infromation? ListView?
You can post that as a separate question, it also depends on your use case - DO you want to persist data? Do you want to use a content provider so that your views refresh automatically when your datasource refreshes. However if you google a simple example for JSON to ListView in Android, I am pretty sure you will find a lot of simple examples from where you can build up. For a starter, check this out.
@Skynet I used your solution in my code here stackoverflow.com/questions/37092206/… But i did something wrong. Please impart a second to review.
0

You could use gson to parse it to an object and then access the properties via that object. So that the "total" would be an int or a double. And the "last" would be a string. Then your quotes would be a List where object would have the properties of each json object item in the jsonarray.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.