Search code examples
androidarraysjsonibeaconibeacon-android

create array from JSON value and pass


I have JSON values storing major & minor_id values in the key value pair and i'm getting the values proper but when i pass the value to check it's checking only last value from JSON and it's not checking all the values from JSON. How can i create array from the values and pass the array of value to check?

JSON:

[{"name":"Test","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major_id":"23695","minor_id":"46901","notification":"","type":"Image","product_image":"server.com/beacon/staging/images/product_images/visualize2.png"},

{"name":"Test1","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major_id":"42583","minor_id":"41770","notification":"","type":"Text","text_text":"<p>test</p>"}]

Parsing & assigning:

 protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                //JSONObject jsonObj = new JSONObject(jsonStr);

                 JSONArray jarray = new JSONArray(jsonStr);

              for (int i = 0; i < jarray.length(); i++) {

                        jObject = jarray.getJSONObject(i);

                        Log.d("jsonObj_Response: ", "> " + jObject);



                         uuid = jObject.getString(TAG_UUID);
                         name = jObject.getString(TAG_NAME);

                         type = jObject.getString(TAG_TYPE);

                         //objFurnitureDesc.id = Integer.parseInt(jsonFurnitureDescObj.getString("id")) ;

                         major_id= Integer.parseInt(jObject.getString("major_id")) ;

                         minor_id= Integer.parseInt(jObject.getString("minor_id")) ;


region_server = new Region("region_server", uuid, major_id, minor_id);  //creating region

 public void onEnteredRegion(final Region region, List<Beacon> beacons) {

     if (region.getIdentifier().equals("region_server")) { 

               notify_image.setVisibility(View.VISIBLE);

   }


          }

Values:

If i have major_id & minor_id

major_id: 23695 minor_id=46901

major_id:42583  minor_id:41170

It's checking last last major & minor_id values of 42583 & 41170 only


Solution

  • use for loop to check for all methods

       JSONArray responseArray=new JSONArray(response);
       List<Integer> majorIdList=new ArrayList<Integer>();
       List<Integer> minorIdList=new ArrayList<Integer>();
    
      for(int i=0;i<responseArray.lenght;i++)
    
      {
           JSONObject jObject=responseArray.getJSONObject(i);
           major_id= Integer.parseInt(jObject.getString("major_id")) ;
    
           minor_id= Integer.parseInt(jObject.getString("minor_id")) ;
           majorIdList.add(major_id);
           minorIdList.add(minor_id);
           Region region_server = new Region("region_server", uuid, major_id, minor_id);  //creating region
    
           onEnteredRegion(region_server,beacons);
      }
    
    
    
     public void onEnteredRegion(final Region region, List<Beacon> beacons)
           {
    
              if (region.getIdentifier().equals("region_server")) 
                { 
    
                 notify_image.setVisibility(View.VISIBLE);
    
                }
           }