Hi,
I have this JSON data...
{
"bar": {
"a": "b",
"x": "y"
}
}
...which is a map[string]map[string]string
However, json.UnMarshal gives me a map[string]interface {}
How do I convert that back into map[string]map[string]string?
I've tried:
1) .(map[string]map[string]string) but get a panic.
2) Range down into the data structure, but I get: "cannot range over type interface {}".
FYI, my program has no way of knowing the names of the fields/keys in advance, but every key and value is a string and thus will fit into map[string]map[string]string.
Here's my code:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
)
func main() {
/*
{
"bar": {
"a": "b",
"x": "y"
}
}
*/
fileContents, err := ioutil.ReadFile("./json.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var f interface{}
err = json.Unmarshal(fileContents, &f)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println(f) // map[bar:map[a:b x:y]]
fmt.Println(reflect.TypeOf(f)) // map[string]interface {}
// now how do I convert f into map[string]map[string]string ?
}