R
R
Roman Rakzin2015-07-27 07:05:25
go
Roman Rakzin, 2015-07-27 07:05:25

Parsing string fields in json in golang is displayed with quotes. How to remove them?

Client sends json like this
{"function":"GroupAdd","data":"New group name"}
I parse json

type struct_json struct { 
      Function string
      Data     json.RawMessage  
   }

    ... 
   var Struct_json = &struct_json{}
    var b_clientMessage =[]byte(clientMessage)
    err := json.Unmarshal(b_clientMessage, Struct_json ) 
    if err != nil {
       log.Println("error:", err)
    }
    
    switch Struct_json.Function {
    case "GroupAdd":
       GroupAdd(Id,string(Struct_json.Data))
    ...
    func GroupAdd(UserId int, Name string){
    	query := "insert into groups(Name,UserId) values (?,?)"
    	if new_query_exec(query, Name, UserId){ //тут записывается в БД
    		Refresh_Groups()	
    	}
    }

As a result, I want a group to be added to the database - The name of the new group, but I get the same value in quotes.
How to get rid of quotes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2015-10-13
@pav5000

The quotes are displayed because you specified the json.RawMessage type for the Data field. This type is designed just to disable parsing of the field, i.e., output the field in its original format. If you replace json.RawMessage with a string, then the quotes will disappear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question