zoukankan      html  css  js  c++  java
  • json 解析以及字符串转

    zhanwei...

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"github.com/tidwall/gjson"
    	"io/ioutil"
    	"log"
    	"net/http"
    )
    
    func httpGet() string {
    	resp, err := http.Get("https://coopera3f7e.com/xxxxx")
    	if err != nil {
    		// handle error
    	}
    
    	defer resp.Body.Close()
    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		// handle error
    	}
    	//fmt.Println(string(body))
    	return string(body)
    }
    
    type Person struct {
    	Name  string `json:"name"`
    	//Hobby string `json:"hobby"`
    }
    type TT struct {
    	T1 []Person `json:"test"`
    }
    
    type FileDes struct {
    	FileId  string `json:"file_id"`
    	//Hobby string `json:"hobby"`
    	Pic_path string `json:"thumbnail_full_path"`
    }
    type TTT struct {
    	T2 []FileDes `json:"5f4f334147cf2103f8c58814"`
    }
    
    func main() {
    	str_json:=httpGet()
    	//log.Println(str_json)
    	value2 := gjson.Get(str_json,"data.5f4f334147cf2103f8c58814")
    	//log.Println("{"5f4f334147cf2103f8c58814":"+value2.Str+"}")
    	
    	//重要,构造成json key:value的格式,为解析做准备,value2.str 最开始==> [{"a":"b"},{"a","b"},{"a","c"}]
    	mdfile := "{"5f4f334147cf2103f8c58814":"+value2.Str+"}"
    	mdfl :=[]byte(mdfile)
    	//str_tmp:=`{"test":[{"Name":"zhangsan","Hobby":"女"},{"Name":"zhangsan","Hobby":"女"},{"Name":"zhangsan","Hobby":"女"}]}`
    	//log.Println(str_tmp)
    	b := []byte(`{"test":[{"Name":"zhangsan","Hobby":"女"},{"Name":"zhangsan","Hobby":"女"},{"Name":"zhangsan","Hobby":"女"}]}`)
    	var p TT
    	err := json.Unmarshal(b, &p)
    	if err != nil {
    		fmt.Println("json err:", err)
    	}
    	log.Println(p)
    	var pp TTT
    	err = json.Unmarshal(mdfl, &pp)
    	if err != nil {
    		fmt.Println("json err:", err)
    	}
    	log.Println(pp)
    
    
    }
    

      

  • 相关阅读:
    握手挥手状态(转)
    牛客笔记
    Redis常见面试题
    SpringBoot整合Redis及Redis工具类撰写(转)
    Netty中FastThreadLocal源码分析(转)
    计算机网络各层涉及协议(转)
    计算机小网络小笔记
    数据库
    操作系统提供的接口
    可重入锁和不可重入锁
  • 原文地址:https://www.cnblogs.com/eiguleo/p/14168946.html
Copyright © 2011-2022 走看看