-3

This is my json value,

{"test":"ruslan","status":"OK"}

How to get "test" value?

And this is my httpclient code to acces api

AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth("user01", "pwd01");
client.get("http://localhost/web/api/getsession", new AsyncHttpResponseHandler() {
      @Override
      public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

        // in this section, I want to store test value from json to a variable

      @Override
      public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
              Log.d("Status", "failure");
          }
      });
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1) `http://localhost/web/` isn't going to work 2) Where is the JSON string in your app? You need to convert a `byte[]` to a string first, it seems. – OneCricketeer Mar 17 '17 at 04:49

3 Answers3

1

you can do it like this

    String json = "{\"test\":\"ruslan\",\"status\":\"OK\"}";
    String test = "";
    try {
        JSONObject jsonObject = new JSONObject(json);
        test = jsonObject.getString("test");
    }catch (JSONException je){
        je.printStackTrace();
    }
    System.out.println("test : " + test);
Prashant
  • 1,593
  • 1
  • 17
  • 32
0

First get ur json from server by connecting to it

DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
    HttpResponse response = httpclient.execute(httppost);           
    HttpEntity entity = response.getEntity();

    inputStream = entity.getContent();
    // json is UTF-8 by default
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null)
    {
        sb.append(line + "\n");
    }
    result = sb.toString();
} catch (Exception e) { 
    // Oops
}
finally {
    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}

now you have your JSON, so what?

Create a JSONObject:

JSONObject jObject = new JSONObject(result);

To get a specific string

String aJsonString = jObject.getString("test");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Avinash Roy
  • 953
  • 1
  • 8
  • 25
0

Here is the sample for how to parse json.

    {
   "sys":
   {
      "country":"GB",
      "sunrise":1381107633,
      "sunset":1381149604
   },
   "weather":[
      {
         "id":711,
         "main":"Smoke",
         "description":"smoke",
         "icon":"50n"
      }
   ],

  "main":
   {
      "temp":304.15,
      "pressure":1009,
   }
}

Java code

 JSONObject sys  = reader.getJSONObject("sys");
country = sys.getString("country");

JSONObject main  = reader.getJSONObject("main");
temperature = main.getString("temp");

For More Detail see This Demo

Roadies
  • 3,309
  • 2
  • 30
  • 46