0

I am building an app in android studio that uses JSON to acess to my postgresql where is my data and I am receiving the data this way:

[
{"id":"1","title":"12 May to 30 Jun"},
{"id":"2","title":"3 Jun to 20 Jun"}
]

I tried to find every where how to use JSONObject or JSONArray for "unlock" the data for pass it to other variables

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • https://stackoverflow.com/questions/25582199/sending-arraylist-from-android-to-php-script-using-json – Jamil Jun 04 '17 at 18:27

3 Answers3

0

After sometime trying and trying I found a way to

                            String finalJson = buffer.toString();

            try {
                JSONArray parentArray = new JSONArray(finalJson);

                int count = 0;
                int[] id = new int[parentArray.length()];
                String[] title = new String[parentArray.length()];
                StringBuffer finalBufferedData = new StringBuffer();
                while (count < parentArray.length())
                {

                    JSONObject finalObject = parentArray.getJSONObject(count);
                    id[count] = finalObject.getInt("id");
                    title[count] = finalObject.getString("title");
                    finalBufferedData.append(id[count] + " - " + title[count] + "\n");
                    count++;

                }
                return finalBufferedData.toString();


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

this way I was able to get the 2 rows from postgresql and show it (later will add it the app sqlite so it doesn't require always to check in my postgresql)

0

Here is the working code:

public void parseJson() {

    // Response from API call
    String response = "[{\"id\":\"1\",\"title\":\"12 May to 30 Jun\"},\n" +
                       "{\"id\":\"2\",\"title\":\"3 Jun to 20 Jun\"}]";

    try {
        JSONArray jsonArray = new JSONArray(response);

        // Get all jsonObject from jsonArray
        for (int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            String id = null, title = null;

            // Id
            if (jsonObject.has("id") && !jsonObject.isNull("id")) {
                id = jsonObject.getString("id");
            }

            // Title
            if (jsonObject.has("title") && !jsonObject.isNull("title")) {
                title = jsonObject.getString("title");
            }

            Log.d("SUCCESS", "JSON Object: " + "\nId: " + id
                                             + "\nTitle: " + title);
        }
    } catch (JSONException e) {
        Log.e("FAILED", "Json parsing error: " + e.getMessage());
    }
}

OUTPUT:

D/SUCCESS: JSON Object: 
           Id: 1
           Title: 12 May to 30 Jun

D/SUCCESS: JSON Object: 
           Id: 2
           Title: 3 Jun to 20 Jun
Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
-2

protected void onPostExecute(String result) { //result parameter should be final so that it can be used in cross thread operation super.onPostExecute(result);

        if (result != null) {
            try {



                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject object = jsonArray.getJSONObject(i);
                    String Brand = object.getString("UserName");

HashMap<String, String> itemList = new HashMap<String, String>();
itemList.put("UserName", Brand);
BrandList.add(itemList);
                }
                **adapter = new SimpleAdapter(Main2Activity.this, BrandList, R.layout.list, new String[]{"UserName"}, new int[]{R.id.txtTitel});
                ((AdapterView<ListAdapter>) listView).setAdapter(adapter);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else {
            Toast.makeText(Main2Activity.this, "Could not get any data.", Toast.LENGTH_LONG).show();
        }
    }
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Sep 07 '18 at 23:54
  • first you need to define jsonArray and jsonobject to read objects from jsonarray – عبده سيرين Sep 10 '18 at 21:08