2017-10-29 1 views
0

私は最初のElmアプリケーションを開発中です。Elm:ローカルJSONファイルをdictにデコードします

ルックアップテーブルとしてローカルのJSONファイルを使用します。このファイルは気象条件(文字列)と提案された衣服(リスト)を一致させます。

EDIT(質問を明確にする):2015年のいくつかの以前の質問に基づいて、Http.getを使用してコンテンツを取得し、次にDecode.dictを使用してDictを作成しようとしました。ローカルファイルを取り込むためにHttpモジュールを使用するのは奇妙に思えます。この方法論は、Http.sendをElmで正しく使用していますか?

また、私のようなJSONファイルで動作するデコーダの例が見つかりません。任意のポインタが評価されるだろう。

JSON

{ 
    "male,10,overcast,light wind...": ["Winter Cap", "Light Jacket"...], 
    "female,30,partly cloudy...": ["Winter Cap", "Sunglasses", ...] 
} 

CODE

type alias ClothingDict = 
    Dict String List 

clothingUrl: String 
clothingUrl = 
    "./clothing.json" 

getClothingDict : Cmd Msg 
getClothingDict = 
    let 
    url = clothingUrl 
    in 
    Http.send SetClothing (Http.get url decodeClothingResponse) 

type alias ClothingResponse = 
    { clothingOptions: ClothingDict } 

decodeClothingResponse : Decoder ClothingResponse 
decodeClothingResponse = 
    Decode.dict ClothingResponse 
+1

にJSONからの文字列のリストをデコーダうデコーダであるinitのフラグとしてJSONを渡す検討? –

答えて

0

Decode.dictまたはdictはdictののキーを復号処理するためにDecoderをとります。 dictは自動的にキーを文字列として抽出します。

私はコードを変換:

decodeClothingResponse : Decoder (Dict String (List String)) 
decodeClothingResponse = 
    dict (list string) 

(list string)List String

関連する問題