2017-12-28 12 views
0

私は、アプリに含めるための数学的アルゴリズムをコーディングしようとしています。複数のレベルを持つJSONをデコードしてデータを保存する方法は?

多くの「レベル」を返すAPIからデータを取得していますが、そのうちの14個が必要です。私はまた、それらを使用するために各 "レベル"の値を格納する必要があります(私はそれらを可能な限り頻繁に更新する必要がありますが、それは私が推測する別のスレッドのためです)。

JSON Sample Screenshot

リンク:ここhttps://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=60&aggregate=3&e=CCCAGG

私はせずに "0" レベル(?アレイ)のデータをフェッチしようとしている方法です。ここ

は、JSONの始まりがどのように見えるかです成功:

struct Root : Decodable { 
    private enum CodingKeys : String, CodingKey { case data = "Data" }  
    let data : Data 
} 

struct Data : Decodable { 
    private enum CodingKeys : String, CodingKey { case zero = "0" } 
    let zero : Zero 
} 

struct Zero : Decodable { 

    private enum CodingKeys : String, CodingKey { 
     case time 
     case open 
     case high 
     case low 
     case close 
    } 
    let time : Double 
    let open : Double 
    let high : Double 
    let low : Double 
    let close : Double 
} 

と...

let marketData0 = try? JSONDecoder().decode(Root.self, from: data) 

0〜13の「レベル」からデータを取得し、それらを使用して計算に使用するにはどうすればよいですか?

+1

JSON文字列の例を含めて解析することをお勧めします。画像は実際の文字列ほどは役に立ちません。 – ColGraff

+0

私はJSONリンクを追加しても問題ありませんか? – Wizzardzz

+0

あなたのリンクには実際の "レベル"の値はありません。 'Data'は単なるJSON配列です。 – ColGraff

答えて

0
import Foundation 

// Response "Data" key array elements 
struct Level: Codable { 
    let time: Double 
    let close: Double 
    let high: Double 
    let low: Double 
    let open: Double 
    let volumefrom: Double 
    let volumeto: Double 
} 

// The whole response (with some keys missing) 
struct Response: Codable { 
    let response: String 
    let type: Int 
    let aggregated: Bool 
    let data: [Level] // Translates the JSON array 

    // custom key names 
    private enum CodingKeys : String, CodingKey { 
    case data = "Data" 
    case response = "Response" 
    case type = "Type" 
    case aggregated = "Aggregated" 
    } 
} 

// The actual JSON String (shortened to two array members) 
let json = """ 
{"Response":"Success", 
"Type":100, 
"Aggregated":true, 
"Data":[ 
    {"time":1514469240,"close":14090.41,"high":14119.99,"low":14077.7,"open":14093.54,"volumefrom":189.71,"volumeto":2696906.84}, 
    {"time":1514469420,"close":14127.84,"high":14131.19,"low":14081.38,"open":14090.41,"volumefrom":197.76,"volumeto":2805972.44}], 
"TimeTo":1514480160, 
"TimeFrom":1514469240, 
"FirstValueInArray":true, 
"ConversionType":{ 
    "type":"direct", 
    "conversionSymbol":""}} 
""" 

// Make the String into a Data and decode it 
if let jsonData = json.data(using: .utf8), 
    let decoded = try? JSONDecoder().decode(Response.self, from: jsonData) { 
    // print all the decoded objects 
    print(decoded) 

    // print the decoded Data object at index 0 
    print("\n", decoded.data[0]) 
} 

// Decoded objects: 

/** Response(response: "Success", type: 100, aggregated: true, data: [ 
    __lldb_expr_15.Level(time: 1514469240.0, close: 14090.41, high: 14119.99, low: 14077.700000000001, open: 14093.540000000001, volumefrom: 189.71000000000001, volumeto: 2696906.8399999999), 
    __lldb_expr_15.Level(time: 1514469420.0, close: 14127.84, high: 14131.190000000001, low: 14081.379999999999, open: 14090.41, volumefrom: 197.75999999999999, volumeto: 2805972.4399999999)]) */ 

// Data object at index 0: 

/** Level(time: 1514469240.0, close: 14090.41, high: 14119.99, low: 14077.700000000001, open: 14093.540000000001, volumefrom: 189.71000000000001, volumeto: 2696906.8399999999) */ 
+0

ありがとうございました!あなたがここで何をしているのかわからない、手動ですべてをタイプするのではなく、APIからデータを取得する必要があるのですか、何か不足していますか?トップに「レスポンス」、「タイプ」、「集計」を指定すると、レスポンスのないデータを単純に使用することはできません。私はJSONの新機能ですので、ごめんなさい – Wizzardzz

+0

完全な例としてJSON文字列を含めていますが、代わりにAPIからデータを取得して、その取得した文字列を使用します。私がやっていることは、JSONのデータ構造を定義し、それをデコードすることです。あなたはキー "データ"を持つ配列を含む主なものを持っています。メインのものが取り出されると、それを使ってSwiftの他の「配列」と同じように「データ」の各メンバーを得ることができます。 – ColGraff

+0

オハイオ州それを手に入れよう!私はあなたのコードをPlaygroundにコピーし、そこから始めて、あなたの例で必要なものを構築します。ありがとうございました – Wizzardzz

関連する問題