获取JSON的值?

时间:2014-11-20 11:56:54

标签: android json web-services

我正在尝试获取我的Web服务返回的JSON值。我从不使用JSON我总是使用SOAP,但今天我需要使用JSON,我不知道如何获得这个值。

我的网络服务返回此JSON:{"cod":999,"msg":"User Data.","return":{"ID":"74","name":"FERNANDO PAIVA","email":"fernando@mydomain.com"}}

我想收到电子邮件,例如,我该怎么做?

我正在尝试这个。

//make a get in web service, return a String with JSON format
public String get(String url){
        String s = "";

        try {
            //executa o get
            HttpGet httpGet = new HttpGet(url);         
            httpGet.addHeader("Authorization", "Basic " + getBasicAuthentication());            
            HttpResponse httpResponse = httpClient.execute(httpGet);
            BufferedReader bReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            //trata o retorno do get
            StringBuilder stringBuilder = new StringBuilder("");
            String line = "";
            String lineSeparator = System.getProperty("line.separator");
            while((line = bReader.readLine()) != null){
                stringBuilder.append(line + lineSeparator);             
            }
            bReader.close();            
            //
            s = stringBuilder.toString();
        } catch (ClientProtocolException e) {           
            e.printStackTrace();
        } catch (IOException e) {           
            e.printStackTrace();
        }

        return s;
    }


//return a value of JSON 
public String getUsuarioByEmail(){
        String url = "mydomain.com.br/json.php?email=fernando@mydomain.com.br";
        String response = httpClient.get(url);
        JSONArray jArray = null;
        String ss = "";
        try {
            JSONObject obj = new JSONObject(response);
            jArray = obj.getJSONArray("return");
            for(int x = 0; x < jArray.length(); x++){
                JSONObject e = jArray.getJSONObject(x);
                ss = e.getString("email");              
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
        return ss;
    }

3 个答案:

答案 0 :(得分:1)

可能这就是你想要的:

try {
            JSONObject obj = new JSONObject(response);
            JSONObject returnObject = obj.getJSONObject("return");
           String email = returnObject.getString("email");
           //so on


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

答案 1 :(得分:1)

你可以试试这个:

JSONObject responseObj = new JSONObject(response);
if (responseObj.optInt("cod") == 99) // Response code validation, add your logic here
{
    JSONObject returnObj = responseObj.optJSONObject("return");
    if (returnObj != null)
    {
         String email = returnObj.optString("email");
    }
}

使用opt***方法不会抛出JSONException,这与get***方法不同,如果映射不存在,则抛出JSONException。根据您的需要,使用适当的方法。

答案 2 :(得分:0)

这就是全部:

//return a value of JSON 
public String getUsuarioByEmail(){
    String url = "mydomain.com.br/json.php?email=fernando@mydomain.com.br";
    String response = httpClient.get(url);
    JSONArray jArray = null;
    String ss = "";
    try {
        JSONObject obj = new JSONObject(response);
        obj = obj.getJSONObject("return");
        ss = obj.getString("email");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
    return ss;
}