-3

My String contains json

result=[{"USER_ID":83,"PROJECT_BY_DETAILS":"An adaptation of a nursery rhyme into a dramatic film"},{"USER_ID":88,"PROJECT_BY_DETAILS":"Test - over ye mountain blue "}]

How to create JSONOBject and JSONarray from this string

I used this code

JSONObject json =new JSONObject(result);
            //Get the element that holds the earthquakes ( JSONArray )
            JSONArray earthquakes = json.getJSONArray("");

i got error

Error parsing data org.json.JSONException: Value [{"USER_ID":83,"PRO
sandeep
  • 15
  • 1
  • 9

4 Answers4

0

If it starts with [ its an array, try with:

JSONArray json =new JSONArray(result);

Difference between JSONObject and JSONArray

Community
  • 1
  • 1
isma3l
  • 3,833
  • 1
  • 22
  • 28
0

use Gson for you to do that.

That Json response is an array you know it because of the square brackets [].

  1. Create a mapping object (a java class) with field USER_ID and PROJECT_BY_DETAILS.

    public class yourClass(){ public String USER_ID; public String PROJECT_BY_DETAILS; }

  2. Create a Type array like so.

    final Type typeYourObject = new TypeToken>(){}.getType();

  3. define your list private

    List yourList;

  4. Using Gson you will convert that array to a List like so

    yourList = gson.fromJson(yourJson, typeYourObject);

  5. with that list later you can do whatever you want. Also with Gson convert it back to JsonArray or create a customs JsonObject.

Pedro Varela
  • 2,296
  • 1
  • 26
  • 32
  • That is all you need. I explain it. The only thing I miss was the instantiation of Gson. like Gson gson = new Gson(); – Pedro Varela Jul 03 '15 at 15:20
0

use this code for your JsonArray:

try {
                        JSONArray json = new JSONArray(YOUR_JSON_STRING);
                         for (int i = 0; i < json.length(); i++) {

                            JSONObject jsonDATA = json.getJSONObject(i);

                            String jsonid = jsonDATA.getInt("USER_ID");
                            String jsondetails = jsonDATA.getString("PROJECT_BY_DETAILS");
                            }
                             } catch (JSONException e) {
                        return null;
                    }
Santiago
  • 1,744
  • 15
  • 23
0

According to my understanding the JSON object looks like this,

{ "RESULT":[ { "USER_ID":83, "PROJECT_BY_DETAILS":"An adaptation of a nursery rhyme into a dramatic film" }, { "USER_ID":88, "PROJECT_BY_DETAILS":"Test - over ye mountain blue " } ] }

You are converting this to a String and you wish to re-construct the JSON object. The decode function in the android-side would be this,

void jsonDecode(String jsonResponse) { try { JSONObject jsonRootObject = new JSONObject(jsonResponse); JSONArray jData = jsonRootObject.getJSONArray("RESULT"); for(int i = 0; i < jData.length(); ++i) { JSONObject jObj = jData.getJSONObject(i); String userID = jObj.optString("USER_ID"); String projectDetails = jObj.optString("PROJECT_BY_DETAILS"); Toast.makeText(context, userID + " -- " + projectDetails,0).show(); } } catch(JSONException e) { e.printStackTrace(); } }

Soumojit Ghosh
  • 921
  • 7
  • 16