FernandoPaiva
FernandoPaiva

Reputation: 4462

Getting values of a JSON?

I'm trying get a value of a JSON that my web service return. I never use JSON I always use SOAP, but today I need use JSON and I dont know how can I get values this.

My web service returns this JSON: {"cod":999,"msg":"User Data.","return":{"ID":"74","name":"FERNANDO PAIVA","email":"[email protected]"}}.

I want get email for example, how can I do this ?

I'm trying this.

//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/[email protected]";
        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;
    }

Upvotes: 0

Views: 65

Answers (3)

ToYonos
ToYonos

Reputation: 16843

You can try this :

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

Using opt*** methods does not throw JSONException, unlike get*** method, which throw JSONException if the mapping does not exist. Use the appropriate method, depending on what you want.

Upvotes: 1

lazy lord
lazy lord

Reputation: 173

May be this is what you want :

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

Upvotes: 1

Davide Pastore
Davide Pastore

Reputation: 8738

That's all:

//return a value of JSON 
public String getUsuarioByEmail(){
    String url = "mydomain.com.br/[email protected]";
    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;
}

Upvotes: 0

Related Questions