0

I'm trying to read two json arrays from same request
JSON FILE:

{
"items": [
  {
    "title": "welcoem home",
    "author": "Charles Dickens"
  },
  {
    "title": "Harry Potter",
    "author": "J rowling"
  }]
}
{
"items1": [
  {
    "title": "welcoem home",
    "author": "Charles Dickens"
  },
  {
    "title": "Harry Potter",
    "author": "J rowling"
  }]
}

and the code I use to read is:

 //Convert JSON to String and gets our app version
    private String getJSON(String JSON_STRING){

        try {
            //Objects
            jsonObject=new JSONObject(JSON_STRING);
            jsonArray=jsonObject.getJSONArray("items");
            //Variables
            int count=0;
            String tempTitle, temAuthor;

            while (count<jsonArray.length()){

                //Reads Row by Row
                JSONObject JO= jsonArray.getJSONObject(count);
                tempTitle= JO.getString("title");
                temAuthor= JO.getString("author");

                //If our App found
                if (Constants.Title.equals(tempTitle)){

                    return temAuthor;
                }

                count++;

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

        return "Not Found!";
    }

but when I call to read the second table like items1 it says no table found? I tried to add a (,) between tables but same, I'm newbie in both php and android so its hard for me to change the code, so it is any easy way for me, I mean any way to change less code to make it work, or I have to change my android method completely ?

1
  • Your json format is wrong. Correct it first Commented Dec 2, 2019 at 20:15

2 Answers 2

1

Try the below JSON code,

{
  "items": [
  {
    "title": "welcoem home",
    "author": "Charles Dickens"
  },
  {
    "title": "Harry Potter",
    "author": "J rowling"
  }]
  ,
  "items1": [
  {
    "title": "welcoem home",
    "author": "Charles Dickens"
  },
  {
    "title": "Harry Potter",
    "author": "J rowling"
  }]
}

You can check the validity of your JSON code in any of the online parser such as http://json.parser.online.fr/

Sign up to request clarification or add additional context in comments.

Comments

0

I found the easiest way for me, @Michael Jaison answer works too, but I fount the easiest way, I just read all joined both json tables like

{table1}###{table2}

then in android I read all file then I split string:

String[] splitedResult = result.split("###");

so I got to tables

splitedResult[0] = { "items": [...
splitedResult[1] = { "items1": [...

Then just passed strings to method to read it :) and whoala it works :)

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.