美文网首页
第12章 处理json/XML

第12章 处理json/XML

作者: yezide | 来源:发表于2020-01-19 01:10 被阅读0次

1、结构体 -> json串

package main

import (
    "encoding/json"
    "fmt"
    // "log"
    "os"
)

type Address struct {
    Type    string
    City    string
    Country string
}

type VCard struct {
    FirstName string
    LastName  string
    Addr []*Address
    Remark    string
}

func main() {
    // 将结构体转换成json str
    addr1 := &Address{"home", "gz", "cn"}
    addr2 := &Address{"comp", "hz", "cn"}
    vc := VCard{"Li", "JingLe", []*Address{addr1, addr2}, "remark1"}
    jsonStr, _ := json.Marshal(vc)
    fmt.Printf("JSON format: %s\n", jsonStr)

    // 生成Writer,可以写入文件
    file, _ := os.OpenFile("/Users/lijingle/Downloads/vcard.json", os.O_CREATE|os.O_WRONLY, 0666)
    defer file.Close()
    enc := json.NewEncoder(file)
    enc.Encode(vc)
    fmt.Printf("JSON from: %v\n", vc)
}

2、 json -> object

package main

import (
    "encoding/json"
    "fmt"
    // "os"
)

type Address struct {
    Type    string
    City    string
    Country string
}

type VCard struct {
    FirstName string
    LastName  string
    Addr []*Address
    Remark    string
}

func main() {
    jsonObj := []byte(`{"FirstName":"Li","LastName":"JingLe","a":"1","Addr":[{"Type":"home","City":"gz","Country":"cn"},{"Type":"comp","City":"hz","Country":"cn"}],"Remark":"remark1"}`)

    // f 指向的值是一个 map,key 是一个字符串,value 是自身存储作为空接口类型的值:
    // 这种方式灵活但很麻烦
    var f interface{}
    json.Unmarshal(jsonObj, &f)
    m := f.(map[string]interface{})
    for k, v := range m {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string value = ", vv)
        case int:
            fmt.Println(k, "is int value = ", vv)
    
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        default:
            fmt.Println(k, "is of a type I don’t know how to handle")
        }
    }

    // 解析到指定构建体. json多了少了也无所谓,试了下能够正常解析
    var card VCard;
    json.Unmarshal(jsonObj, &card)
    fmt.Printf("%s\n", card.FirstName)
}

相关文章

  • 第12章 处理json/XML

    1、结构体 -> json串 2、 json -> object

  • Jdk8 suportted JacksonUtil

    XML、JSON 处理的烦恼 java中处理json与xml数据时,有各种各样的第三方库供用户选择。其中具有代表性...

  • Python 常用模块

    OS,SYS,logging, subprocess,hashlib加密,json ,pickle,XML处理,c...

  • QQ空间音乐免费API接口说明

    这个接口是以JSON和XML做返回值处理的,默认为返回JSON。 API接口返回值为JSON: 返回为 JSON ...

  • 2018-12-17数据

    结构数据 xml json 非结构化数据 html处理方法 xpath 正则表达式 json 怎么找json的ur...

  • 06-数据提取-概念和分类

    爬虫中数据的分类:结构化数据:json,xml等json:可以通过键值对获取的数据处理方式:json、jsonpa...

  • golang中xml的动态解析

    今天在处理xml解析的时候,遇到一个动态处理的问题如下: Param的结构不固定,但是xml又没有json的Raw...

  • 数据格式

    XML和JSON数据格式 json数据格式 {"":"", "":"", "":""} json xml对比 1....

  • 异常JSON格式化,取巧方式

    在开发中,JSON 数据交互非常平凡,行业的标准处理xml就是JSON,但是如果客户使用非标准化的JSON就会出现...

  • json文件转换为XML文件

    友情链接:JSON与XML互转 json文件: XML文件

网友评论

      本文标题:第12章 处理json/XML

      本文链接:https://www.haomeiwen.com/subject/rwwlzctx.html