2016-12-23 5 views
1

javaを使用して大学の水文プロジェクトの天気データを記録しようとしています。私はすでにJSONファイルからデータにアクセスするために、次のコードを使用することを試みたjsonobject keysとしてタイムスタンプ付きのjsonを読み取る

{ 
    "1482439800":{ 
     "hu":92, 
     "te":-2.9, 
     "dp":-4.5, 
     "pr":1028.4, 
     "pa":null, 
     "ws":1.4, 
     "wd":180 
    }, 
    "1482440100":{ 
     "hu":92, 
     "te":-2.9, 
     "dp":-4.5, 
     "pr":1028.4, 
     "pa":null, 
     "ws":1.4, 
     "wd":180 
    } 
} 

: データは、次のように最後の24時間json file 5分間隔(一例)としてフォーマットされる

これは湿度(hu)のデータを返したものの、一見無作為な順序であるため、やや成功しました。

私の質問のために:どのように時刻を読み取り、オブジェクト[] []内の最新のものから古いものまでの気象データと一緒に戻すのですか?

何か助けていただければ幸いです。 ありがとうございます。

答えて

0

答えがありがとうございましたが、結局私は少しシンプルなルートに行くことにしました。 すべてのキー名を取得してソートし、対応するデータキーをキーで読み込みました。また、データがnullのため頻繁にエラーが発生したので、そのための保護も追加しました(実際の数値として必要です)。

public static Object[][] getstation1(){ 
     Object[][] data = null; 
     try { 
      JSONObject json = new JSONObject(readUrl("http://netzwerk.wetter.com/api/stationdata/14091/2/")); 
      System.out.println("Fetching "+"http://netzwerk.wetter.com/api/stationdata/14091/2/"); 
      String[] times = json.getNames(json); 
      Arrays.sort(times); 
      data = new Object[times.length][8]; 
      for (int i = 0; i < times.length; i++){ 
       Date temp = new Date((long)Integer.parseInt(times[i])*1000); 
       data[i][0] = temp; 
       if (json.getJSONObject(times[i]).isNull("hu")){ 
        data[i][1] = 0; 
       } else { 
        data[i][1] = json.getJSONObject(times[i]).getDouble("hu"); 
       } 
       if (json.getJSONObject(times[i]).isNull("te")){ 
        data[i][2] = 0; 
       } else { 
        data[i][2] = json.getJSONObject(times[i]).getDouble("te"); 
       } 
       if (json.getJSONObject(times[i]).isNull("dp")){ 
        data[i][3] = 0; 
       } else { 
        data[i][3] = json.getJSONObject(times[i]).getDouble("dp"); 
       } 
       if (json.getJSONObject(times[i]).isNull("pr")){ 
        data[i][4] = 0; 
       } else { 
        data[i][4] = json.getJSONObject(times[i]).getDouble("pr"); 
       } 
       if (json.getJSONObject(times[i]).isNull("pa")){ 
        data[i][5] = 0; 
       } else { 
        data[i][5] = json.getJSONObject(times[i]).getDouble("pa"); 
       } 
       if (json.getJSONObject(times[i]).isNull("ws")){ 
        data[i][6] = 0; 
       } else { 
        data[i][6] = json.getJSONObject(times[i]).getDouble("ws"); 
       } 
       if (json.getJSONObject(times[i]).isNull("wd")){ 
        data[i][7] = 0; 
       } else { 
        data[i][7] = json.getJSONObject(times[i]).getDouble("wd"); 
       }     
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }  
     return data; 
    } 
1

ソートされたマップは、Object[][]よりも適切です。すぐに

TreeMap<String, Object> sorted = new TreeMap<>(json.toMap()); 

しかし、(タイムスタンプがすべて同じ長さであるとして、あなたのケースでは、おそらく罰金)それはアルファベットソートされます。

あなたは、型指定されたマップに結果をソートするために、もう少し作業を行うことができます:あなた本当には、それがソートされます一度、あなたのデータを再マッピングすることができObject[][]が必要な場合は

TreeMap<Date, Map<String, Double>> byDate = json.toMap().entrySet().stream() 
    .collect(Collectors.toMap(
     e -> new Date(Long.valueOf(e.getKey()) * 1000), 
     e -> (Map) e.getValue(), 
     (a, b) -> {throw new IllegalArgumentException("Duplicate key " + a);}, 
     TreeMap::new 
    )); 

Object[][] data = sorted.entrySet().stream().map(e -> new Object[] {e.getKey(), e.getValue()}).toArray(Object[][]::new); 

jacksonまたはgsonのようなオブジェクトマッパーの使用を検討してください。

関連する問題