This commit is contained in:
huangsimin 2018-12-20 18:42:35 +08:00
parent 4fddfe2863
commit 93cf2a6841
2 changed files with 31 additions and 17 deletions

View File

@ -4,8 +4,11 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
"strconv"
"strings"
"sync" "sync"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"474420502.top/test/logdb" "474420502.top/test/logdb"
@ -208,6 +211,21 @@ func parserAndSendMQ(adp IParser, adrChan chan logdb.ADResonse, wg *sync.WaitGro
} }
} }
// IsJSON 测试数据是否为JSON
func IsJSON(data string) bool {
var v interface{}
if gjson.Unmarshal([]byte(data), &v) != nil {
data = strings.Trim(data, "\"")
unq, _ := strconv.Unquote("\"" + data + "\"")
data = unq
if gjson.Unmarshal([]byte(data), &v) != nil {
// return "", errors.New("json invalid")
return false
}
}
return true
}
// NewADParser 创建一个ADParser的类, 包含很多传到终端的所有结构 // NewADParser 创建一个ADParser的类, 包含很多传到终端的所有结构
func NewADParser(SpiderID int) *ADParser { func NewADParser(SpiderID int) *ADParser {
adp := &ADParser{} adp := &ADParser{}

View File

@ -2,10 +2,9 @@ package parser
import ( import (
"encoding/json" "encoding/json"
"errors"
"log" "log"
"regexp" "regexp"
"strconv"
"strings"
"testing" "testing"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -33,7 +32,9 @@ func (tt *Toutiao) ToDoParser(adstring string) (presult string, err error) {
defer func() { defer func() {
if ierr := recover(); ierr != nil { if ierr := recover(); ierr != nil {
log.Println(ierr) log.Println(presult, ierr)
err = ierr.(error)
presult = ""
} }
}() }()
@ -43,26 +44,21 @@ func (tt *Toutiao) ToDoParser(adstring string) (presult string, err error) {
} }
for _, data := range adlist { for _, data := range adlist {
label := data[0:10]
log.Println("test json -----------------------")
var v interface{}
log.Println(label, gjson.Unmarshal([]byte(data), &v), gjson.Parse(data).IsObject()) if IsJSON(data) {
return "", errors.New("json invalid")
data = strings.Trim(data, "\"") }
unq, _ := strconv.Unquote("\"" + data + "\"")
log.Println(label, gjson.Unmarshal([]byte(data), &v), gjson.Parse(data).IsObject())
data = unq
log.Println(label, gjson.Unmarshal([]byte(data), &v), gjson.Parse(data).IsObject())
result := gjson.Parse(data) result := gjson.Parse(data)
// log.Println(result.String()) // log.Println(result.String())
if result.Get("mixed").Exists() {
log.Println("this is mixed") for i, rkey := range []string{"app", "mixed", "image_recom"} {
log.Println(result.Get("mixed.track_url").Exists()) root := result.Get("mixed")
if root.Exists() {
}
} }
json.Unmarshal([]byte(data), &v)
// spew.Dump(v) // spew.Dump(v)
regexp.Compile(``) regexp.Compile(``)
} }