包含字符串和整数的Golang映射

I am trying to create a JSON string from a map using JSON.Marshal() in golang. However, the int values are being displayed as strings surrounded by double quotes.

My code is outputting:

{ "age":
    {
        "$gt":"22",
        "$lt":"20"
    },
  "location":
    {
        "$eq":"london"
    },
  "name":{
        "$eq":"fred"
    }
}

instead of

{ "age":
    {
        "$gt":22,
        "$lt":20
    },
  "location":
    {
        "$eq":"london"
    },
  "name":{
        "$eq":"fred"
    }
}

I am using:

var output_map = map[string]map[string]string{}

//Populate map here

output_json, err := json.Marshal(output_map)

if err!= nil {
    fmt.Println("Error encoding JSON")
}

fmt.Println(output_json)

My understanding is that JSON.Marshal() will print the integers correctly if they are supplied but my map won't contain integers. I could change my map to map[string]map[string]int{} but then it wouldn't contain the string values for 'name' and 'location'.

The ultimate problem is that I need the map to contain both int and string values. Some sort of map[string]map[string]{}.

How can I achieve this? Thank you in advance.

Harry

If you cannot describe your data with a properly typed struct then consider using a map with values of type interface{} (essentially any type):

output_map := map[string]map[string]interface{}{}

For example:

output_map := map[string]map[string]interface{}{
  "age": {
    "$gt": 18,
  },
  "location": {
    "eq": "London",
  },
}
bytes, err := json.MarshalIndent(&output_map, "", "  ")
if err != nil {
  panic(err)
}
// {
//   "age": {
//     "$gt": 18
//   },
//   "location": {
//     "eq": "London"
//   }
// }

Of course, using the interface{} type is not a best-practice; however, it's sometimes the only way to accomplish certain things.

I agree with maerics,

map[string]interface{} would be the way to go, if you have to avoid structs.

From your content I assume, that you are dealing with mongodb-queries.
So maybe the following code helps.

If you are going to query mongodb from go I would recommend the mgo-driver mgo.v2.
It implements a custom type bson.M which is the same as mentioned above, but works also for querying the db.

Beside this, it makes the code more readable.

Sample:

package main

import (
    "fmt"
    "gopkg.in/mgo.v2/bson"
    "encoding/json"
)

func main() {

    bsonMap := bson.M{
        "age": bson.M{
            "$gt": 22,
            "$lt": 20,
        },
        "location": bson.M{"$eq": "london",},
        "name":     bson.M{"$eq": "fred"},
    }

    bytes, err := json.Marshal(bsonMap)

    if err != nil {
        panic(err)
    }

    fmt.Println(string(bytes))
}

Your code works as expected: map[string]map[string]string{} contains only string values, so json.Marshal(interface{}) return json string with marshaled string values.

I think you should use struct instead of map. Just create a struct, something like this:

type Age struct {
    Gt  int `json:"gt"`
    Lt  int `json:"lt"`
}

type Person struct {
    Age         Age     `json:"age"`
    Location    string  `json:"location"`
    Name        string  `json:"name"`
}

And just marshal it with standart json.Marshal(interface{}) method. (in this code json tags isn't required, but you can use it if you want to change names of fields in serialized json string)

I create test.go file with following code in main():

person := Person{
    Age{
        22,
        20,
    },
    "London",
    "Fred",
}

serialized, e := json.Marshal(person)
if e != nil {
    fmt.Println(e)
    return
}

fmt.Println(string(serialized))

and it returns this:

{
    "age":
    {
        "gt":22,
        "lt":20
    },
    "location":"London",
    "name":"Fred"
}

P.S. I can't recommend you to use map[string]map[string]interface{} because in this case you need to have custom Unmarshaller (to fill up your map back). With structs you can unmarshal it by this way:

deserialized := Person{}
e = json.Unmarshal(serialized, &deserialized)
if e != nil {
    fmt.Println("can't deserialize:", e)
    return
}

We can declare variable for mix mapping with integer,string as below.

var variable_name = map[string]interface{}{}

e.g.

var variable_name = map[string]interface{}{
        "name": c.String("name"),
        "age":  c.Int("age"),
}