package main
import (
"encoding/json"
"github.com/emirpasic/gods/maps/linkedhashmap"
)
func GetGames(){
type GameDetails struct {
Name string `json:"nm"`
Id int `json:"id"`
ThumbUrl string `json:"tmb_ul"`
Requirement linkedhashmap.Iterator `json:"reqs,omitempty"`
}
games := new(GameDetails)
games.Name = "Stronghold Crusader"
games.Id =1120
games.ThumbUrl="picture/eeee.jpg"
//this is JSON in database:
requirement:=`{"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}`
n := linkedhashmap.New()
err:=n.FromJSON([]byte(requirement))
if err != nil {
print(err)
}
result:=n.Iterator()
games.Requirement=result
mkJson,err:= json.Marshal(games)
if err != nil {
print(err)
}
print(string(mkJson))
}
I have tried this way but doesn’t work.(linkedhashmap)
now, how can append “requirement” to games?
1-unmarshal to map[string]string doesn’t work, Because I want JSON as it is
-we haven’t sort map from Golang version 1 and later
2-unmarshal to struct doesn’t work too because we can’t add automatically struct field if an added field in “requirement” JSON
thanks, but what will happen if we add an aditional field in json
and not in code? like this : {"shader":3 ,"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}
I’m just curious. Why is order important? http://www.json.org/ states the following
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).