1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package main
import ( "encoding/json" "github.com/gin-gonic/gin" "github.com/oliveagle/jsonpath" "log" "net/http" )
func main() { address := "localhost:1024" router := gin.Default() router.LoadHTMLGlob("templates/*") router.POST("/parse",parse) router.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{ "title": "Main website", }) }) router.Run(address) }
type User struct { Content string `json:"content"` Expression string `json:"expression"` } func parse(c *gin.Context) { content := c.PostForm("content") log.Println("content:", content) jsonObj := User{} c.BindJSON(&jsonObj)
var json_data interface{} json.Unmarshal([]byte(jsonObj.Content), &json_data) log.Println("传入数据:", jsonObj.Content) log.Println("表达式:", jsonObj.Expression)
res, err := jsonpath.JsonPathLookup(json_data, jsonObj.Expression) if (jsonObj.Content == "") { c.JSON(http.StatusOK, gin.H{ "message": "请输入json", "parsed": "{}", "expression": jsonObj.Expression, }) } else if (err != nil) { log.Println(err) c.JSON(http.StatusOK, gin.H{ "message": res, "parsed": "解析异常", "expression": jsonObj.Expression, }) } else { log.Printf("%v", &jsonObj) c.JSON(http.StatusOK, gin.H{ "message": "", "parsed": res, "expression": jsonObj.Expression, }) } }
|