【问题标题】:how to json object value in blackberry + java如何在黑莓+ java中json对象值
【发布时间】:2013-12-03 13:17:39
【问题描述】:

我正在获取 json 对象我想从 json 中获取 formatted_address 值我将如何获得?

您可以在访问此服务器时检查 json 值 http://maps.googleapis.com/maps/api/geocode/json?latlng=28.6353080,77.2249600&sensor=true

   private void getLocationFromGoogleMaps() {
                        try {
                            StreamConnection s = null;
                            InputStream iStream = null;     
                            long lat=(long) 28.04;
                            long longt=(long) 77.0484;
                            s=(StreamConnection)javax.microedition.io.Connector.open("http://maps.googleapis.com/maps/api/geocode/json?latlng=28.6353080,77.2249600&sensor=true");//&deviceside=false&ConnectionType=mds-public"

                            HttpConnection con = (HttpConnection)s; 
                            con.setRequestMethod(HttpConnection.GET);
                            con.setRequestProperty("Content-Type", "//text");
                             int status = con.getResponseCode();
                             if (status == HttpConnection.HTTP_OK)
                             {
                                 iStream=s.openInputStream();                    
                                 int len=(int) con.getLength();
                                 byte[] data = new byte[8000];                    
                                 byte k;
                                 String result="";
                                 while((k = (byte)iStream.read()) != -1) {
                                     result = result+(char)k;
                                 }  

                                // System.out.println("------0-----------------------+result"+result);
                                 try {
                                      JSONObject jsonObjectMapData=new JSONObject(result);   
                                      System.out.println("-------nav--------"+jsonObjectMapData.getJSONArray("formatted_address"));
                                      synchronized(Application.getEventLock())
                                      {
                                    Dialog.alert("------------------------"+jsonObjectMapData.getJSONObject("formatted_address"));
                                      }
                                      //lblLoc.setText("Location information currently unavilable");
        //                           JSONArray  jsonaryPlaceMark = jsonObjectMapData.getJSONArray("Placemark");
        //                          JSONObject address= jsonaryPlaceMark.getJSONObject(0);
        //                           String placeName=address.getString("address");
        //                           if(placeName!=null)
        //                               lblLoc.setText(address.getString("address"));
        //                           else
        //                               lblLoc.setText("Location information currently unavilable");
                                 } catch (Exception e) {
                                     lblLoc.setText("location information Currently Unavilable");
                                 }
                             }
                        }catch (Exception e) {
                            System.out.println(e);
                            lblLoc.setText("location information Currently Unavilable");
                        }        
                    }

【问题讨论】:

    标签: java json blackberry


    【解决方案1】:

    用这段代码替换try块

    JSONObject jsonObjectMapData=new JSONObject(result);
    JSONArray arr=jsonObjectMapData.getJSONArray("results");
    int size=arr.length();
    JSONObject objectArray[]=new JSONObject[size];//this will store all the json object from array
    String formatted_address[]=new String[size];//Declare a String array which will store the value of formatted_address
    for(int i=0;i<size;i++)
    {
    objectArray[i]=arr.getJSONObject(i);//now you have objectArray which contains all the object 
     formatted_address[i]=objectArray[i].getString("formatted_address");//Now formated_address string array will store all .Do what you have to do
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多