Saraz
Saraz

Reputation: 544

Unable to parse JSON array in Android app

I have an Android app that uses bar code scanner library. All is working good, until I have created an Activity that is called after the scanning is done.

That Activity uses this JSON array:

<?php
$barCodes = array(
array(
    "id" => 123456,
    "format" => "upc_1"
),
array(
    "id" => 39123439,
    "format" => "upc_2"
),
array(
    "id" => 12345670,
    "format" => "upc_3"
  )
);

echo json_encode($barCodes); ?>

and the code to parse this JSONArray is:

   String scanResult;

   Intent intent = getIntent();
   scanResult = intent.getStringExtra("result");


HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://someAddress.php");


  try {

   HttpResponse response = httpclient.execute(httppost);
   String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();


   JSONObject object = new JSONObject(jsonResult);

   JSONArray jArray = object.getJSONArray("barCodes");

   JSONObject json_data;
   int id ;
   String format ;
   String ret_format[] = new String[jArray.length()];
   int ret_id[] = new int[jArray.length()];

   for(int i=0;i<jArray.length();i++){
        json_data = jArray.getJSONObject(i);

        id =    json_data.getInt("id");
        format = json_data.getString("format");

        ret_id[i] = id;
        ret_format[i] = format;
       }


      int scanInt = Integer.parseInt(scanResult);

      switch(scanInt) {

      case 123456:
          textView.setText(ret_id[0] + " - " + ret_format[0]);  
      break;

      case 39123439:
          textView.setText(ret_id[1] + " - " + ret_format[1]);  
      break;

      case 12345670:
          textView.setText(ret_id[2] + " - " + ret_format[2]);  
      break;

      default:
          textView.setText("Result doesn't match the codes available...");
      break;

      }

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

}

private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
         while ((rLine = rd.readLine()) != null) {
          answer.append(rLine);
           }
        }

        catch (IOException e) {
            e.printStackTrace();
         }
        return answer;
       }

app is working without errors, but it is not displaying any result from the switch statement. I do not know where is the problem. Someone here may have had the same problem, so please help me.

Thanks.

Upvotes: 0

Views: 418

Answers (1)

Ajay Kumar Meher
Ajay Kumar Meher

Reputation: 1952

What I feel is your JSON structure is not valid.

Have a look on this structure

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

JSON is based on {}, [], :, ,

I insist you check the jArray = object.getJSONArray("barCodes"); and try to print the log. like jArray.toString();

You will know exactly what the array is having inside.

Upvotes: 1

Related Questions