2016-12-06 8 views
2

APIレスポンスからjsonを読み込んでいて、json値(string、null、bool)内に複数のデータ型が存在するという問題が発生しました。さらに、キーの中には、データを型に読み込むことをより困難にする文字列またはヌルのいずれかの値を持つものがあります。私は扱いやすさのためにすべてを文字列に変換したい。私はグーグルの他の例に基づいてタイプスイッチを作った。私は、これを行う最も簡単な方法か、もっと単純なアプローチが欠けているのかどうか疑問に思っています。 " { { T_INT変数タイプのjsonを文字列に変換する

package main 

import (
"encoding/json" 
"fmt" 
"strconv" 
) 

func main() { 

json_byte := []byte(`{"response":[{"t_int":1, "t_bool": true, "t_null_or_string": null}, {"t_int":2, "t_bool": false, "t_null_or_string": "string1"}]}`) 

//unmarshal the json to data structure using interface for variable data types 
data_json := make(map[string][]map[string]interface{}) //create a structure to hold unmarshalled json 
if err := json.Unmarshal(json_byte, &data_json); err != nil { 
    panic(err) 
} 
fmt.Println("json_data: ", data_json) 

//Iterate over data structure and convert Bool, Int, and Null types to string 
var v_conv string        // temporary holding for converted string values 
data_map := make(map[string]string)    // temporary holding for converted maps 
data_final := make([]map[string]string, 0, 100) // final holding for data converted to strings 

for _, v := range data_json { //v is the value of the "response": key which is a slice of maps 
    for _, v2 := range v { //v2 is one of the maps in the slice of maps 
     for k3, v3 := range v2 { //k3 and v3 are the keys and values inside the map 
      fmt.Println("k3: ", k3, "v3: ", v3) 
      switch v_type := v3.(type) { 
      case nil: 
       v_conv = "" 
      case bool: 
       v_conv = strconv.FormatBool(v3.(bool)) 
      case int: 
       v_conv = strconv.Itoa(v3.(int)) 
      case string: 
       v_conv = v3.(string) 
      case float64: 
       v_conv = strconv.FormatFloat(v3.(float64), 'f', 0, 64) 
      default: 
       fmt.Println("vtype unknown: ", v_type) //have to use v_type since it is declared 
       v_conv = "" 
      } 
      data_map[k3] = v_conv //append a new map key/value pair both as strings 
      fmt.Println("data_map: ", data_map) 
     } 
     data_final = append(data_final, data_map) // after each cycle through the loop append the map to the new list 
     fmt.Println("data_final: ", data_final) 
    } 
} 
} 

最終形式マップ [ "1"、 "t_bool": "真"、 "t_null_string"" "T_INT"}」のスライスを希望しました「: "2"、 "t_bool": "偽"、 "t_null_string": "この答えのために文字列1" }]

答えて

2

私はJSONはあなたの例では(の一部)の一例であると仮定していますあなたのJSON入力。 この場合、JSONには特定の構造があります。既知のデータ型でどの属性が使用されているのか、どの属性が動的であるのかを知っています。 たとえば、あなたは下のResponseObjのようになめらかにあなたのJSONを非整列化できます。あなたのデータは次のようになります

package main 

import (
    "encoding/json" 
    "fmt" 
) 

type ResponseObj struct { 
    Response []Item `json:"response"` 
} 

type Item struct { 
    TInt int   `json:"t_int"` 
    TBool bool  `json:"t_bool"` 
    TMixed interface{} `json:"t_null_or_string"` 
} 

func main() { 

    json_byte := []byte(`{"response":[{"t_int":1, "t_bool": true, "t_null_or_string": null}, {"t_int":2, "t_bool": false, "t_null_or_string": "string1"}]}`) 

    data_json := ResponseObj{} 
    if err := json.Unmarshal(json_byte, &data_json); err != nil { 
     panic(err) 
    } 
    fmt.Printf("%+v\n", data_json) 
} 

{ 
    Response: 
    [ 
     { 
      TInt:1 
      TBool:true 
      TMixed:<nil> 
     } 
      { 
      TInt:2 
      TBool:false 
      TMixed:string1 
     } 
    ] 
} 

そして、はい、混合型の属性のためにあなたが実行されますタイプアサーション(または、あなたのケースまたはその両方のnilとの比較)。

あなたのJSONは予測不可能な種類の混乱である可能性は低いです。ほとんどの場合、コアのコア構造を使い、残っている混在した種類のインタフェース{}を使用することができます。

これが役に立ちます。

+0

私はこれが彼のケースでは最高の解決策だと言います。 –

+0

これはずっときれいです。ありがとうございました。 –

関連する問題