0

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":"fernando@mydomain.com"}}.

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/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;
    }
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118

3 Answers3

1

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();
        } 
lazy lord
  • 173
  • 1
  • 2
  • 13
1

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.

ToYonos
  • 16,469
  • 2
  • 54
  • 70
0

That's all:

//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;
}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53