如何用这个Json获取数据

时间:2017-09-25 20:32:37

标签: android json

我想显示以下JSON数据:

{"success":true,"message":"","result":{"Bid":3924.01000000,"Ask":3925.00000000,"Last":3924.00000000}}

我是android的新手,在所有教程中,我以某种方式访问​​这些数据,但我只想显示“Last”数据图,但我的代码显示整个“结果”字符串。

String firstname;

TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView)findViewById(R.id.name);

    new AsyncTaskParseJson().execute();

}


// you can make this class as another java file so it will be separated from your main activity.
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

    final String TAG = "AsyncTaskParseJson.java";

    // set your json string url here
    String yourJsonStringUrl = "https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc";

    // contacts JSONArray
    JSONArray dataJsonArr = null;

    protected void onPreExecute() {}

    protected String doInBackground(String... arg0) {

        try {

            // instantiate our json parser
            JSONParser jParser = new JSONParser();

            // get json string from url
            JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);


            firstname = json.getString("result");

            Log.e(TAG, "Last: " + firstname);


        }
        catch (JSONException e) {
            e.printStackTrace();

        }

        return null;
    }

    @Override
    protected void onPostExecute(String strFromDoInBg) {

        tv.setText(""+firstname);

    }
}

2 个答案:

答案 0 :(得分:0)

您没有拨打网络电话。你只是解析你的网址。你需要进行网络通话。你可以使用截击。在你的onCreate方法

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.name);
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://bittrex.com/api/v1.1/public/getticker?market=usdt-btc";

 // Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
    new Response.Listener<String>() {
  @Override
  public void onResponse(String response) {

      JSONObject json = new JSONObject(response);
    try {
        JSONObject result = json.getJSONObject("result");
        Double last = result.getDouble("Last");
        tv.setText(String.valueOf(last));
    } catch (JSONException e) {
        e.printStackTrace();
    }
  }
  }, new Response.ErrorListener() {
 @Override
   public void onErrorResponse(VolleyError error) {

  }
});
// Add the request to the RequestQueue.
 queue.add(stringRequest);


}

您需要在Manifest.xml中定义Internet权限

<uses-permission android:name="android.permission.INTERNET" />

您需要为此http请求添加排球库。将其包含在应用的build.gradle中

dependencies {
 .
 .
 compile 'com.android.volley:volley:1.0.0'
 .
 .
}

答案 1 :(得分:-1)

我认为你应该能够做到这一点

JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
return "" + json.get("result").getDouble("Last");

字符串从doInBackground返回到strFromDoInBg变量中的onPostExecute,但如果您愿意:

firstname = "" + json.get("result").getDouble("Last");

也可以使用