Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取此JSON对象并将其解析为Android textview_Java_Android_Json - Fatal编程技术网

Java 如何获取此JSON对象并将其解析为Android textview

Java 如何获取此JSON对象并将其解析为Android textview,java,android,json,Java,Android,Json,我正在使用CryptoCompare提供的API。我需要从这个JSON对象获取符号和价格 { "Response": "Success", "Message": "Do not take life too seriously. You will never get out of it alive.", "Data": [ { "Symbol": "USD", "Price": 5660.94, "Open24Hour": 5155.13,

我正在使用CryptoCompare提供的API。我需要从这个JSON对象获取符号价格

{
  "Response": "Success",
  "Message": "Do not take life too seriously. You will never get out of it alive.",
  "Data": [
    {
      "Symbol": "USD",
      "Price": 5660.94,
      "Open24Hour": 5155.13,
      "LastUpdateTS": 1507885905,
      "Volume24Hours": 222438.875,
      "Volume24HoursTo": 1214073220
    },
    {
      "Symbol": "EUR",
      "Price": 4757.16,
      "Open24Hour": 4318.19,
      "LastUpdateTS": 1507885905,
      "Volume24Hours": 26488.4023,
      "Volume24HoursTo": 120264888
    }
  ],
  "Type": 100
}

我建议您将数据存储到
列表中

List<String> symbol = new ArrayList<String>();
List<String> price = new ArrayList<String>();
因此,初始化两个
列表

List<String> symbol = new ArrayList<String>();
List<String> price = new ArrayList<String>();
List symbol=new ArrayList();
标价=新的ArrayList();
然后您可以在这里存储数据

try {

    JSONObject json = new JSONObject(response);
    JSONArray jArray = json.getJSONArray("Data");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject object = jsonArray.getJSONObject(i);
        symbol.add(object.getString("Symbol"));
        price.add(object.getString("Price"));

    }

} catch (JSONException e) {
    e.printStackTrace();
}
试试看{
JSONObject json=新的JSONObject(响应);
JSONArray jArray=json.getJSONArray(“数据”);
for(int i=0;i

这可能对您有所帮助

我建议您将数据存储到
列表

List<String> symbol = new ArrayList<String>();
List<String> price = new ArrayList<String>();
因此,初始化两个
列表

List<String> symbol = new ArrayList<String>();
List<String> price = new ArrayList<String>();
List symbol=new ArrayList();
标价=新的ArrayList();
然后您可以在这里存储数据

try {

    JSONObject json = new JSONObject(response);
    JSONArray jArray = json.getJSONArray("Data");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject object = jsonArray.getJSONObject(i);
        symbol.add(object.getString("Symbol"));
        price.add(object.getString("Price"));

    }

} catch (JSONException e) {
    e.printStackTrace();
}
试试看{
JSONObject json=新的JSONObject(响应);
JSONArray jArray=json.getJSONArray(“数据”);
for(int i=0;i
这可能会帮助您

如何做

  • 如果在代码中遇到
    {}
    ,可以使用
    JSONObject
    对其进行解析

  • 如果在代码中遇到
    []
    ,可以使用
    JSONArray
    对其进行解析

  • 如果在代码中遇到
    []
    ,则可以使用
    for loop
    从中获取值

  • 您应该在代码中使用
    try catch

试试这个

try {
        JSONObject jsonObject = new JSONObject(response);
        String Response = jsonObject.optString("Response");
        JSONArray Data = jsonObject.optJSONArray("Data");
        for (int i = 0; i < Data.length(); i++) {
            JSONObject jo = Data.optJSONObject(i);
            String Symbol = jo.optString("Symbol");
            String Price = jo.optString("Price");
            String Open24Hour = jo.optString("Open24Hour");
            String LastUpdateTS = jo.optString("LastUpdateTS");
            String Volume24Hours = jo.optString("Volume24Hours");
            String Volume24HoursTo = jo.optString("Volume24HoursTo");
        }
} catch (JSONException e) {
        e.printStackTrace();
}
试试看{
JSONObject JSONObject=新JSONObject(响应);
字符串响应=jsonObject.optString(“响应”);
JSONArray Data=jsonObject.optJSONArray(“数据”);
对于(int i=0;i
怎么做

  • 如果在代码中遇到
    {}
    ,可以使用
    JSONObject
    对其进行解析

  • 如果在代码中遇到
    []
    ,可以使用
    JSONArray
    对其进行解析

  • 如果在代码中遇到
    []
    ,则可以使用
    for loop
    从中获取值

  • 您应该在代码中使用
    try catch

试试这个

try {
        JSONObject jsonObject = new JSONObject(response);
        String Response = jsonObject.optString("Response");
        JSONArray Data = jsonObject.optJSONArray("Data");
        for (int i = 0; i < Data.length(); i++) {
            JSONObject jo = Data.optJSONObject(i);
            String Symbol = jo.optString("Symbol");
            String Price = jo.optString("Price");
            String Open24Hour = jo.optString("Open24Hour");
            String LastUpdateTS = jo.optString("LastUpdateTS");
            String Volume24Hours = jo.optString("Volume24Hours");
            String Volume24HoursTo = jo.optString("Volume24HoursTo");
        }
} catch (JSONException e) {
        e.printStackTrace();
}
试试看{
JSONObject JSONObject=新JSONObject(响应);
字符串响应=jsonObject.optString(“响应”);
JSONArray Data=jsonObject.optJSONArray(“数据”);
对于(int i=0;i
看看这个

try {

            JSONObject objresponse=new JSONObject("{\"Response\":\"Success\",\"Message\":\"Do not take life too seriously. You will never get out of it alive.\",\"Data\":[{\"Symbol\":\"USD\",\"Price\":5660.94,\"Open24Hour\":5155.13,\"LastUpdateTS\":1507885905,\"Volume24Hours\":222438.875,\"Volume24HoursTo\":1.21407322E+09},{\"Symbol\":\"EUR\",\"Price\":4757.16,\"Open24Hour\":4318.19,\"LastUpdateTS\":1507885905,\"Volume24Hours\":26488.4023,\"Volume24HoursTo\":120264888.0}],\"Type\":100}");
            JSONArray arrayData=objresponse.getJSONArray("Data");
            for (int i=0;i<arrayData.length();i++){
                JSONObject obj=arrayData.getJSONObject(i);
                String symbol=obj.getString("Symbol");
                float price=(float)obj.getLong("Price");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
试试看{
JSONObject objresponse=new JSONObject(“{”Response\“:“Success\”,“Message\”:“不要把生活看得太重。你永远不会活着离开它。”,“Data\”:[{“Symbol\”:“USD\”,“Price\”:5660.94,““Open24Hour\”:5155.13,““lastUpdates\”:1507885905,““Volume24Hours\”:222438.875,““Volume24HoursTo\”:1.07322+09”,“{\“:4757.16,\'Open24Hour\':4318.19,\'lastUpdates\':1507885905,\'Volume24Hours\':26488.4023,\'Volume24HoursTo\':120264888.0}],\'Type\':100}”);
JSONArray arrayData=objresponse.getJSONArray(“数据”);
对于(inti=0;i检查此项

try {

            JSONObject objresponse=new JSONObject("{\"Response\":\"Success\",\"Message\":\"Do not take life too seriously. You will never get out of it alive.\",\"Data\":[{\"Symbol\":\"USD\",\"Price\":5660.94,\"Open24Hour\":5155.13,\"LastUpdateTS\":1507885905,\"Volume24Hours\":222438.875,\"Volume24HoursTo\":1.21407322E+09},{\"Symbol\":\"EUR\",\"Price\":4757.16,\"Open24Hour\":4318.19,\"LastUpdateTS\":1507885905,\"Volume24Hours\":26488.4023,\"Volume24HoursTo\":120264888.0}],\"Type\":100}");
            JSONArray arrayData=objresponse.getJSONArray("Data");
            for (int i=0;i<arrayData.length();i++){
                JSONObject obj=arrayData.getJSONObject(i);
                String symbol=obj.getString("Symbol");
                float price=(float)obj.getLong("Price");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
试试看{
JSONObject objresponse=new JSONObject(“{”Response\“:“Success\”,“Message\”:“不要把生活看得太重。你永远不会活着离开它。”,“Data\”:[{“Symbol\”:“USD\”,“Price\”:5660.94,““Open24Hour\”:5155.13,““lastUpdates\”:1507885905,““Volume24Hours\”:222438.875,““Volume24HoursTo\”:1.07322+09,”,“{:4757.16,“开放24小时”:4318.19,“最新更新集”:1507885905,“卷24小时”:26488.4023,“卷24小时”:120264888.0}],“类型”:100});
JSONArray arrayData=objresponse.getJSONArray(“数据”);

对于(int i=0;i你尝试了什么?我的朋友,我建议你至少尝试一些东西。这是一项你可以轻松实现谷歌搜索的任务。让我给你一个提示:你尝试了什么?我的朋友,我建议你至少尝试一些东西。这是一项你可以轻松实现谷歌搜索的任务。让我给你一个提示:谢谢你的回答,它帮助了我的思考你的答案帮助我记住了你。这是我最好的答案。很高兴这个答案行得通。那么请点击勾选@emeUshiwuHow接受这个答案?看那个@emeUshiwuYou是受欢迎的。谢谢。这是我最好的答案。很高兴这个答案行得通。那么请点击勾选@emeUshiwuHow接受这个答案?看那个@emeUshiwuYou是受欢迎的。