How to handle Response JSON have custom field with out key?
Query Api and response a custom JSON, how to Unmarshal it. the sample JSON:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
The key is a hex string. So how to handle it with golang convention, anyone can help me?
Below is my try test code:
c.OnResponse(func(r *colly.Response)
jsonData := r.Body
fmt.Println(string(jsonData))
fmt.Println("==================")
//parse bitcoin json
jsonMap := make(map[string]interface)
err := json.Unmarshal(byte(jsonData), &jsonMap)
if err != nil
panic(err)
fmt.Println(jsonMap)
dumpMap("", jsonMap)
)
func dumpMap(space string, m map[string]interface)
for k, v := range m
if mv, ok := v.(map[string]interface); ok
fmt.Printf(" "%v": n", k)
dumpMap(space+"t", mv)
fmt.Printf("n")
else
fmt.Printf("%v %v : %vn", space, k, v)
and go run cmd/main.go
, the console is print here:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 75494521080,
"n_tx": 3493,
"total_received": 3493574275763
==================
map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:map[n_tx:3493 total_received:3.493574275763e+12 final_balance:7.549452108e+10]]
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
final_balance : 7.549452108e+10
n_tx : 3493
total_received : 3.493574275763e+12
Do I need customised unmarshal func
to get string key? If I use 14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu
as key I can't easily to access. I just want to know how handle it.
go
add a comment |
Query Api and response a custom JSON, how to Unmarshal it. the sample JSON:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
The key is a hex string. So how to handle it with golang convention, anyone can help me?
Below is my try test code:
c.OnResponse(func(r *colly.Response)
jsonData := r.Body
fmt.Println(string(jsonData))
fmt.Println("==================")
//parse bitcoin json
jsonMap := make(map[string]interface)
err := json.Unmarshal(byte(jsonData), &jsonMap)
if err != nil
panic(err)
fmt.Println(jsonMap)
dumpMap("", jsonMap)
)
func dumpMap(space string, m map[string]interface)
for k, v := range m
if mv, ok := v.(map[string]interface); ok
fmt.Printf(" "%v": n", k)
dumpMap(space+"t", mv)
fmt.Printf("n")
else
fmt.Printf("%v %v : %vn", space, k, v)
and go run cmd/main.go
, the console is print here:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 75494521080,
"n_tx": 3493,
"total_received": 3493574275763
==================
map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:map[n_tx:3493 total_received:3.493574275763e+12 final_balance:7.549452108e+10]]
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
final_balance : 7.549452108e+10
n_tx : 3493
total_received : 3.493574275763e+12
Do I need customised unmarshal func
to get string key? If I use 14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu
as key I can't easily to access. I just want to know how handle it.
go
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43
add a comment |
Query Api and response a custom JSON, how to Unmarshal it. the sample JSON:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
The key is a hex string. So how to handle it with golang convention, anyone can help me?
Below is my try test code:
c.OnResponse(func(r *colly.Response)
jsonData := r.Body
fmt.Println(string(jsonData))
fmt.Println("==================")
//parse bitcoin json
jsonMap := make(map[string]interface)
err := json.Unmarshal(byte(jsonData), &jsonMap)
if err != nil
panic(err)
fmt.Println(jsonMap)
dumpMap("", jsonMap)
)
func dumpMap(space string, m map[string]interface)
for k, v := range m
if mv, ok := v.(map[string]interface); ok
fmt.Printf(" "%v": n", k)
dumpMap(space+"t", mv)
fmt.Printf("n")
else
fmt.Printf("%v %v : %vn", space, k, v)
and go run cmd/main.go
, the console is print here:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 75494521080,
"n_tx": 3493,
"total_received": 3493574275763
==================
map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:map[n_tx:3493 total_received:3.493574275763e+12 final_balance:7.549452108e+10]]
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
final_balance : 7.549452108e+10
n_tx : 3493
total_received : 3.493574275763e+12
Do I need customised unmarshal func
to get string key? If I use 14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu
as key I can't easily to access. I just want to know how handle it.
go
Query Api and response a custom JSON, how to Unmarshal it. the sample JSON:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
The key is a hex string. So how to handle it with golang convention, anyone can help me?
Below is my try test code:
c.OnResponse(func(r *colly.Response)
jsonData := r.Body
fmt.Println(string(jsonData))
fmt.Println("==================")
//parse bitcoin json
jsonMap := make(map[string]interface)
err := json.Unmarshal(byte(jsonData), &jsonMap)
if err != nil
panic(err)
fmt.Println(jsonMap)
dumpMap("", jsonMap)
)
func dumpMap(space string, m map[string]interface)
for k, v := range m
if mv, ok := v.(map[string]interface); ok
fmt.Printf(" "%v": n", k)
dumpMap(space+"t", mv)
fmt.Printf("n")
else
fmt.Printf("%v %v : %vn", space, k, v)
and go run cmd/main.go
, the console is print here:
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 75494521080,
"n_tx": 3493,
"total_received": 3493574275763
==================
map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:map[n_tx:3493 total_received:3.493574275763e+12 final_balance:7.549452108e+10]]
"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
final_balance : 7.549452108e+10
n_tx : 3493
total_received : 3.493574275763e+12
Do I need customised unmarshal func
to get string key? If I use 14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu
as key I can't easily to access. I just want to know how handle it.
go
go
edited Nov 14 '18 at 8:36
kit
1,1063816
1,1063816
asked Nov 14 '18 at 5:06
xds2000xds2000
9961013
9961013
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43
add a comment |
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43
add a comment |
1 Answer
1
active
oldest
votes
you can unmarshal it into map, so you can get generated key as a key of map
https://play.golang.org/p/IfEjjvKakpu
package main
import (
"encoding/json"
"fmt"
"log"
)
var input = `"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
`
type object struct
FinalBalance uint64 `json:"final_balance"`
NTX uint64 `json:"n_tx"`
TotalReceived uint64 `json:"total_received"`
func main()
var result map[string]object;
err := json.Unmarshal(byte(input), &result);
if err != nil
log.Fatal(err)
fmt.Printf("result: %+v", result)
// result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293506%2fhow-to-handle-response-json-have-custom-field-with-out-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can unmarshal it into map, so you can get generated key as a key of map
https://play.golang.org/p/IfEjjvKakpu
package main
import (
"encoding/json"
"fmt"
"log"
)
var input = `"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
`
type object struct
FinalBalance uint64 `json:"final_balance"`
NTX uint64 `json:"n_tx"`
TotalReceived uint64 `json:"total_received"`
func main()
var result map[string]object;
err := json.Unmarshal(byte(input), &result);
if err != nil
log.Fatal(err)
fmt.Printf("result: %+v", result)
// result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972]
add a comment |
you can unmarshal it into map, so you can get generated key as a key of map
https://play.golang.org/p/IfEjjvKakpu
package main
import (
"encoding/json"
"fmt"
"log"
)
var input = `"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
`
type object struct
FinalBalance uint64 `json:"final_balance"`
NTX uint64 `json:"n_tx"`
TotalReceived uint64 `json:"total_received"`
func main()
var result map[string]object;
err := json.Unmarshal(byte(input), &result);
if err != nil
log.Fatal(err)
fmt.Printf("result: %+v", result)
// result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972]
add a comment |
you can unmarshal it into map, so you can get generated key as a key of map
https://play.golang.org/p/IfEjjvKakpu
package main
import (
"encoding/json"
"fmt"
"log"
)
var input = `"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
`
type object struct
FinalBalance uint64 `json:"final_balance"`
NTX uint64 `json:"n_tx"`
TotalReceived uint64 `json:"total_received"`
func main()
var result map[string]object;
err := json.Unmarshal(byte(input), &result);
if err != nil
log.Fatal(err)
fmt.Printf("result: %+v", result)
// result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972]
you can unmarshal it into map, so you can get generated key as a key of map
https://play.golang.org/p/IfEjjvKakpu
package main
import (
"encoding/json"
"fmt"
"log"
)
var input = `"14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu":
"final_balance": 61914248289,
"n_tx": 3472,
"total_received": 3479994002972
`
type object struct
FinalBalance uint64 `json:"final_balance"`
NTX uint64 `json:"n_tx"`
TotalReceived uint64 `json:"total_received"`
func main()
var result map[string]object;
err := json.Unmarshal(byte(input), &result);
if err != nil
log.Fatal(err)
fmt.Printf("result: %+v", result)
// result: map[14AcKEr19gHJvgwQhK7sfFm6YJGmoZZoqu:FinalBalance:61914248289 NTX:3472 TotalReceived:3479994002972]
answered Nov 14 '18 at 5:38
iHelosiHelos
614
614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293506%2fhow-to-handle-response-json-have-custom-field-with-out-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just unmarshall to a interface. And format/handle it from there.
– majidarif
Nov 14 '18 at 5:16
@majidarif i just update the question and add more backgroud concerns to this question. welcome give some advise.
– xds2000
Nov 14 '18 at 5:38
There is no point in trying to unmarshall this directly to a struct because the key could and does start with a number. Unmarshall it to a string map with the value as an object of struct.
– majidarif
Nov 14 '18 at 5:43