How to check string is in json format

后端 未结 8 1485
广开言路
广开言路 2020-12-24 02:17

I want to create a function to receive an input string which can be string in json format or just a string. For example, something easy like following function.



        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 02:36

    how about you use something like this:

    if err := json.Unmarshal(input, temp_object); err != nil {
        fmt.Println("it's normal string!")
    } else {
        fmt.Println("it's json!")
    }
    

提交回复
热议问题