【问题标题】:How to use Json on android studio?如何在 Android Studio 上使用 Json?
【发布时间】:2019-10-07 01:17:28
【问题描述】:

下面是我的 json 文件结构。 我想画图(x轴:时间,y轴:“关闭”值)

{ “符号”:“AAPL”, "stock_exchange_short": "纳斯达克", "timezone_name": "美国/纽约", “盘中”:{ 2018-10-19 15:59:00: { “打开”:“219.49”, “关闭”:“219.23”, “高”:“219.61”, “低”:“219.19”, “卷”:“302415”, }, 2018-10-19 15:58:00: { “打开”:“219.62”, “关闭”:“219.48”, “高”:“219.70”, “低”:“219.48”, “卷”:“173762”, }, 2018-10-19 15:57:00: { “打开”:“219.54”, “关闭”:“219.64”, “高”:“219.66”, “低”:“219.46”, “卷”:“130992”, }, 2018-10-19 15:56:00: { “打开”:“219.71”, “关闭”:“219.57”, “高”:“219.77”, “低”:“219.57”, “卷”:“113398”, }, ... } }
@Override
public void onResponse(JSONObject response) {
      JsonArray arr = response.getJsonArray("intraday")??

我想要得到的是包含“数据”作为键和“关闭值”作为每次值的哈希图。谁能告诉我如何获得它

【问题讨论】:

  • “数据”作为键是什么意思?表示您要“打开”、“关闭”、“低”、“音量”作为键吗?

标签: android android-studio


【解决方案1】:

OnResponse() 上将您的JSONArrayListener 更改为JSONObjectListener。另外,像这样在对象中添加您的日期时间戳。

从这里:

 {
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
    2018-10-19 15:59:00: {
        "open": "219.49",
        "close": "219.23",
        "high": "219.61",
        "low": "219.19",
        "volume": "302415",
    },
    2018-10-19 15:58:00: {
        "open": "219.62",
        "close": "219.48",
        "high": "219.70",
        "low": "219.48",
        "volume": "173762",
    },
    2018-10-19 15:57:00: {
        "open": "219.54",
        "close": "219.64",
        "high": "219.66",
        "low": "219.46",
        "volume": "130992",
    },
    2018-10-19 15:56:00: {
        "open": "219.71",
        "close": "219.57",
        "high": "219.77",
        "low": "219.57",
        "volume": "113398",
    },
    ...
}

}

{
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
    "data": [
        {
            "open": "219.49",
            "close": "219.23",
            "high": "219.61",
            "low": "219.19",
            "volume": "302415",
            "datetimestamp": "2018-10-19 15:59:00"
        },
        {
            "open": "219.62",
            "close": "219.48",
            "high": "219.70",
            "low": "219.48",
            "volume": "173762",
            "datetimestamp": "2018-10-19 15:59:00"
        },
        {
            "open": "219.54",
            "close": "219.64",
            "high": "219.66",
            "low": "219.46",
            "volume": "130992",
            "datetimestamp": "2018-10-19 15:59:00"
        },
        {
            "open": "219.71",
            "close": "219.57",
            "high": "219.77",
            "low": "219.57",
            "volume": "113398",
            "datetimestamp": "2018-10-19 15:59:00"
        }
    ]
}

}

然后通过这样做获取数据(顺便说一句,我使用的是AndroidNetworking):

 AndroidNetworking.get("your URL")
            .addHeaders("your Header")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    try{

                        //getting intraday as a JSONObject
                        JSONObject jsonObject = response.getJSONObject("intraday");

                         HashMap<String, String> yourMap = new HashMap();   


                        //getting the data as a JSONArray as given on my example
                        JSONArray jsonArray = jsonObject.getJSONArray("data");

                        for (int x = 0;x< jsonArray.length();x++){

                            JSONObject jsonObject1 = jsonArray.getJSONObject(x);

                            //getting the data inside the json object
                            double open = jsonObject1.getDouble("open");
                            double close = jsonObject1.getDouble("close");
                            double high = jsonObject1.getDouble("high");
                            double low = jsonObject1.getDouble("low");
                            int volume = jsonObject1.getInt("volume"); //or getDouble it depends on your data
                            String dateTimeStamp = jsonObject1.getString("datetimestamp");

                            try {
                            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            Date date = df.parse(dateTimeStamp);
                           SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                           String convertedDateTimeStamp = sdf.format(date);
                           } catch (ParseException e) {
                           e.printStackTrace(); 
                          }

                            yourMap.put(convertedDateTimeStamp, String.valueOf(close))
                        }
                    }catch (Throwable throwable){
                        throwable.printStackTrace();
                    }
                }

                @Override
                public void onError(ANError anError) {

                }
            });

【讨论】:

  • 他为什么要换intraday to a JSONArray?在您的新 JSON format intraday 中是 JSONObject('data' 是 JSONArray)。不是吗?
  • @JakirHossain 抱歉,我错过了输入。我会很快改变它。谢谢你提醒我
【解决方案2】:

您不能对所有关闭值使用相同的键('data')。

Hash map 键是唯一的。如果添加重复的key,则会被覆盖。

我在下面的答案中给出了一个带有唯一键(时间)的hashmap 示例。

你可以像下面这样解析你的 JSON。

@Override
public void onResponse(JSONObject response) {
     JSONObject intradayObj = response.getJSONObject("intraday");

     HashMap<String, String> yourMap = new HashMap();   

     Iterator<?> keys = intradayObj.keys();
     while(keys.hasNext()) {
         String key = (String)keys.next();
         if (intradayObj.get(key) instanceof JSONObject ) {

             JSONObject dateTimeJsonObj = intradayObj.optJSONObject(key);
             if (dateTimeJsonObj == null) {
                continue;
             }
             // get close value 
             String closeValue = dateTimeJsonObj.getString("close")
             Log.d("TAG", "Your Close value "+closeValue);

             // you can others value like close
             String openValue = dateTimeJsonObj.getString("open") 
             // ....


             // here you can store close value
             yourMap.put(getTime(key), closeValue)
         }
     }

      // finally your hashmap will look like (x-axis : time, y-axis : "close" value)
      // ("15:59:00", "219.23")
      // ("15:58:00", "219.48")
      // ("15:57:00", "219.64")
      // ("15:56:00", "219.57")

}

String date-time 获取时间

public String getTime(String dateTime){
   try {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = df.parse(dateTime);
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    String shortTimeStr = sdf.format(date);
   } catch (ParseException e) {
      e.printStackTrace(); 
   }
   return shortTimeStr;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2015-12-04
    相关资源
    最近更新 更多