从JSON值创建数组并传递

时间:2014-10-29 12:26:26

标签: android arrays json ibeacon ibeacon-android

我有JSON值存储主要和&键值对中的minor_id值,我得到的值正确,但是当我传递值来检查它是否只检查来自JSON的最后一个值而且它没有检查来自JSON的所有值。如何从值创建数组并传递值数组来检查?

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>"}]

解析&amp;分配:

 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);

   }


          }

值:

如果我有major_id&amp; minor_id

major_id: 23695 minor_id=46901

major_id:42583  minor_id:41170

它正在检查最后一个主要&amp; minor_id值为42583&amp;仅限41170

1 个答案:

答案 0 :(得分:1)

使用for循环来检查所有方法

   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);

            }
       }