婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Golang 實(shí)現(xiàn)interface類(lèi)型轉(zhuǎn)string類(lèi)型

Golang 實(shí)現(xiàn)interface類(lèi)型轉(zhuǎn)string類(lèi)型

熱門(mén)標(biāo)簽:壽光微信地圖標(biāo)注 外呼系統(tǒng)顯本地手機(jī)號(hào) 評(píng)價(jià)高的400電話辦理 阿克蘇地圖標(biāo)注 百度地圖標(biāo)注后傳給手機(jī) excel地圖標(biāo)注分布數(shù)據(jù) 涿州代理外呼系統(tǒng) 外呼系統(tǒng)用什么卡 電話機(jī)器人軟件免費(fèi)

看代碼吧~

// Strval 獲取變量的字符串值
// 浮點(diǎn)型 3.0將會(huì)轉(zhuǎn)換成字符串3, "3"
// 非數(shù)值或字符類(lèi)型的變量將會(huì)被轉(zhuǎn)換成JSON格式字符串
func Strval(value interface{}) string {
	var key string
	if value == nil {
		return key
	}
	switch value.(type) {
	case float64:
		ft := value.(float64)
		key = strconv.FormatFloat(ft, 'f', -1, 64)
	case float32:
		ft := value.(float32)
		key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
	case int:
		it := value.(int)
		key = strconv.Itoa(it)
	case uint:
		it := value.(uint)
		key = strconv.Itoa(int(it))
	case int8:
		it := value.(int8)
		key = strconv.Itoa(int(it))
	case uint8:
		it := value.(uint8)
		key = strconv.Itoa(int(it))
	case int16:
		it := value.(int16)
		key = strconv.Itoa(int(it))
	case uint16:
		it := value.(uint16)
		key = strconv.Itoa(int(it))
	case int32:
		it := value.(int32)
		key = strconv.Itoa(int(it))
	case uint32:
		it := value.(uint32)
		key = strconv.Itoa(int(it))
	case int64:
		it := value.(int64)
		key = strconv.FormatInt(it, 10)
	case uint64:
		it := value.(uint64)
		key = strconv.FormatUint(it, 10)
	case string:
		key = value.(string)
	case []byte:
		key = string(value.([]byte))
	default:
		newValue, _ := json.Marshal(value)
		key = string(newValue)
	}
	return key
}

補(bǔ)充:golang json 為map[string] interface{}

json字符串:

{"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0,"w":"還"}]},{"bg":0,"cw":[{"sc":0,"w":"有點(diǎn)"}]},{"bg":0,"cw":[{"sc":0,"w":"眼熟"}]}]}

需要將json格式中的w字段取出來(lái),并且拼成結(jié)果串進(jìn)行展示

1、從json數(shù)組中獲取ws

2、ws是數(shù)組,數(shù)組元素為object

3、cw是數(shù)組,數(shù)組元素為object

4、w是string

5、從cw遍歷獲取w字段

解析代碼:

func RecResultJsonToPlain() {
    var recResult string
    var dat map[string]interface{}
    json.Unmarshal([]byte(json_str), dat)
 
    if v, ok := dat["ws"]; ok {
        ws := v.([]interface{})
        for i, wsItem := range ws {
            wsMap := wsItem.(map[string]interface{})
            if vCw, ok := wsMap["cw"]; ok {
                cw := vCw.([]interface{})
                for i, cwItem := range cw {
                    cwItemMap := cwItem.(map[string]interface{})
                    if w, ok := cwItemMap["w"]; ok {
                        recResult = recResult + w.(string)
                    }
                }
            }
        }
    }
    fmt.Println(recResult)
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • Java中InputSteam怎么轉(zhuǎn)String
  • 淺談Java中String的常用方法
  • Java實(shí)用工具之StringJoiner詳解
  • golang 的string與[]byte轉(zhuǎn)換方式
  • go語(yǔ)言map與string的相互轉(zhuǎn)換的實(shí)現(xiàn)
  • Java基礎(chǔ)入門(mén)語(yǔ)法--String類(lèi)

標(biāo)簽:銅川 重慶 雞西 欽州 蘭州 梅河口 吐魯番 汕頭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang 實(shí)現(xiàn)interface類(lèi)型轉(zhuǎn)string類(lèi)型》,本文關(guān)鍵詞  Golang,實(shí)現(xiàn),interface,類(lèi)型,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Golang 實(shí)現(xiàn)interface類(lèi)型轉(zhuǎn)string類(lèi)型》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Golang 實(shí)現(xiàn)interface類(lèi)型轉(zhuǎn)string類(lèi)型的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 铁力市| 滦南县| 吉安县| 康平县| 海门市| 塘沽区| 南和县| 琼结县| 昭苏县| 凤翔县| 神木县| 通山县| 文安县| 贺州市| 南雄市| 若羌县| 黑山县| 吐鲁番市| 峡江县| 博客| 阿克苏市| 依安县| 武隆县| 平和县| 北川| 兴国县| 宁安市| 阿图什市| 永川市| 德钦县| 凤城市| 洪雅县| 井陉县| 海阳市| 普陀区| 漠河县| 衡阳市| 新乡县| 南充市| 余干县| 云和县|