0

How to store jsonarray to arraylist string? please any help is much appreciated. this is my jsonarray

{  
   "ids":[  
      {  
         "noid":"2"
      },
      {  
         "noid":"3"
      }
   ],
   "success":1
}

I want to store the values of identity "noid" in arraylist. here is my code in android.

protected String doInBackground(String... args) {

                // Check for success tag
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("qid", qsid));

                    // getting product details by making HTTP request
                    // Note that product details url will use GET request
                    JSONObject json = jParser.makeHttpRequest(
                            main_url, "GET", params);

                    // check your log for json response
                    Log.d("Single Product Details", json.toString());

                    // json success tag
                    success = json.getInt("success");
                    if (success == 1) {
                        // successfully received product details
                        JSONArray idSet = json
                                .getJSONArray("ids"); // JSON Array
                            for(int i = 0; i < idSet.length(); i++) {
                                JSONObject outputId = idSet.getJSONObject(i);

                                arrayList.add(outputId.getString("noid").toString());
                            }
                    }else{
                        Toast.makeText(getApplicationContext(), json.getString("result"), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

        return null;
    }

and here is my php file.

<?php
require_once 'mobile_question_query.php';

$j = array();

if(isset($_GET["qid"])){


$id = $_GET['qid'];


$fetchallqObject = new Questions();

if(!empty($id)){
        $json_array = $fetchallqObject->fetchAllQuestionsID($id);

        echo json_encode($json_array);
}else{

    $j['result'] = "No id!";
    echo json_encode($j);
}
}else{
    echo "no id found";
}

?>

if i check the array if arraylist is empty, it return always true.

3
  • use gson parsing Commented Sep 22, 2018 at 19:31
  • Your code looks fine, what issue you have ?? Commented Sep 22, 2018 at 19:32
  • my issue is the arraylist which i want to store my jsonarray is always empty Commented Sep 22, 2018 at 19:53

1 Answer 1

1

Use Gson library.

List<String> list= new Gson().fromJson(idSet, new TypeToken<ArrayList<String>>(){}.getType());
Sign up to request clarification or add additional context in comments.

1 Comment

is Gson has the library?? want to try this

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.