Suddenly found a very useful Golang JSON library

Source: Internet
Author: User
Tags ticket
This is a creation in Article, where the information may have evolved or changed.

Json

I don't want to explain what JSON is.

Library nameGJSON

Point here to star thank you little brother

Download

$ go get -u github.com/tidwall/gjson

What's the difference from the official JSON parsing package?

The official package is a whole piece of data packaged/parsed together.
Have the following data

struct {  str1  str2}json :{str1:"xx",str2:"xx"}

If you want to get str1 alone, it's troublesome.

The official Demo

package mainimport "github.com/tidwall/gjson"const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}`func main() {    value := gjson.Get(json, "name.last")    println(value.String())}

Output:Prichard

I wrote the super detailed example!

To show you the JSON I want to parse, I ask you to be afraid:

The first {"RichText": {"data": {"items": [{"desc": "Breaking!!! On the eve of the dog-abusing festival on Singles Day, there is a list of Tanwali that is full of malice. We look look~ This list single out, each road hero lay gun countless! Men and women have said that they have finally found their single reason! ———— Dead Division line ———— Warrior your melancholy temperament has betrayed you, this evil sword to ward away, practice you will have a female ticket soldier brother don't be sad! And you have friends! Teacher, leave me to do homework after class, real mushroom cool ~ Boyfriend was you sent off the red card? Look back at this list, you really shot the real en, our journey, is Star (Huo) Chen (gai) Big (Dan) Sea (Shen) Alive a literary single Wang, pack away bear children while cool go ...                        My uncle and aunt are still alone. Tomorrow, a person to buy tickets for the movie, a person to play repeatedly (kill a pair is a pair), do a quiet beauty male "," End ": 0," image ": { "Key": "293634E25FC4C886C957203DC8C9734D", "name": "6a3fecd2-f565-4ff1-ba03-943170e5a301 (1 ). jpg "," url ":" Http://nos-yx.netease.com/yixinpublic/pr_6cntqgf6tlv8yr6eybbbxg==_1447149809_772110 "}," Start ": 0," SubTitle ":" Breaking!!! On the eve of the dog-abusing festival on Singles Day, there is a list of Tanwali that is full of malice. We look look~ This list single out, each road hero lay gun countless! Men and women have said that they have finally found their single reason! ———— Dead Division line ———— Warrior your melancholy temperament has betrayed you, this evil sword to ward away, practice you will have a female ticket soldier brother don't be sad! And you have friends! Teacher, leave me to do homework after class, real mushroom cool ~ Boyfriend was you sent off the red card?Head look at this list, you really shot in the real en, our journey, is Star (Huo) Chen (gai) Big (Dan) Sea (Shen) Alive a literary single Wang, pack away bear children while cool go ... Uncle and aunt are still alone. Tomorrow, a person to buy tickets, a person to play repeatedly see (Kill a pair is a pair), do a quiet beauty male, "Subsubtype": 0, "title": "Wang!" Unlock the mysteries of your single years!        "}]}," subtype ": 101}}//the second {" audio ": {" duration ": 3189, "MD5": "7ac7585a546a171b39a1be95c43d84b0", "name": "60F9D8F4-16A6-4ECA-B8E6-5E51C39F38691.AAC", "size": 1102 1, "url": "http://nos-yx.netease.com/yixinpublic/pr_zwx0ir6blam67ccfxn8urw==_1495775574_233545677"}, "Locati On ": {" coordinate ":" 28.228272,112.938888 "," title ":" Changsha "}," text ":" Hello "," version ": 1}//the third fourth one The same as the second, do not write ... It's too long.

Golang code (these four strings of JSON can be solved together, so a piece of code can be done)

Package Mainimport "Github.com/tidwall/gjson" Func Main () {jsonstr1: = ' {' RichText ': {"data": {"items": [{"desc": " Want to shoot a star fan can not star temperament how to do! Do you learn to go out and dress well? Paparazzi 360-degree candid can also maintain the temperament, how is it done? Study together. What's the most important thing about a star going out? Install Mystery (BI)! When the eyes are godless, wear a pair of sunglasses. When you have no makeup on your face, wear a mask and only reveal your charming eyes! If necessary, wear a hat, not only can play handsome, but also can cover the plane after a deep sleep messy hairstyle. Looking at the pictures above, do you know how to improve the temperament of the photos? Right! Just cover your face! The most common is wearing a mask, beautiful big eyes, very charming it? When there is no mask, the mystery doubles by blocking half the face with your hands. If you have hair, use it, tease! Mask can also allow you to multiply the temperament, lazy expression with a slippery skin, you are a girl who knows how to live! Did you learn how to shoot a mysterious masked photo? Click to read "end": 0, "image": {"key": "D2ed467715d06ae4b80b13e4eaff3eeb", "name": "Timg (4). jpg", "url": "/http" nos-yx.netease.com/yixinpublic/pr_0ok_9bi3l3z9b8felxt-jw==_1487148473_212641347 "}," Linkurl ":"/http Show.yixin.im/task/taskpage.html?sharesid=f6facd47763e9ae911cefbb036779c64&shareversion=1 "," Start ": 0," SubTitle ":" Want to shoot a star fan can not star temperament how to do! Do you learn to go out and dress well? Paparazzi 360-degree candid can also maintain the temperament, how is it done? Study together. What's the most important thing about a star going out? Install Mystery (BI)! When the eyes are godless, wear a pair of sunglasses. When you have no makeup on your face, wear a mask and only reveal your charming eyes! If necessary, wear a hat, not only can play handsome, but also can cover the plane after a deep sleep messy hairstyle. Looking at the pictures above, do you know how to improve the temperament of the photos? Right! Just cover your face! The most common is wearing a mask, beautiful big eyes, very charming it? When there is no mask, the mystery doubles by blocking half the face with your hands. If you have hair, use it, tease! Apply mask also can let your temperament doubled, lazy expression with slippery skin, you are a understandGirls who have to live! Did you learn how to shoot a mysterious masked photo? Click to read "," Subsubtype ": 0," title ":" No star's life, but can have star disease "}"}, "Subtype": 101}} ' JSONSTR2: = ' {"Images": [{"MD5": " 83b25df0edbddcde2769f1855b06b423 "," size ": 107241," url ":" Http://nos-yx.netease.com/yixinpublic/pr_1qd4af7j14_ nfcfpqplwaw==_1489977060_238390862 "}]," text ":" Take advantage of this warbler fly grass long, the sun coincides with the season, the small partners remember to take time out of the hustle and bustle of the city, Picnic, flowers, fly kites, enjoy the eight of the Sihai spring bar! "} ' JSONSTR3: = ' {" audio ": {" duration ": 3189," MD5 ":" 7ac7585a546a171b39a1be95c43d84b0 "," Name ":" 60f9d8f4-16a6-4eca-b8e6-5e51c39f38691.aac "," size ": 11021," url ":" Http://nos-yx.netease.com/yixinpublic/pr_ zwx0ir6blam67ccfxn8urw==_1495775574_233545677 "}," location ": {" coordinate ":" 28.228272,112.938888 "," title ":" Changsha "} , "text": "Hello", "Version": 1} ' JSONSTR4: = ' {"RichText": {"data": {"items": [{"desc": "Breaking!!! On the eve of the dog-abusing festival on Singles Day, there is a list of Tanwali that is full of malice. We look look~ This list single out, each road hero lay gun countless! Men and women have said that they have finally found their single reason! ———— Dead Division line ———— Warrior your melancholy temperament has betrayed you, this evil sword to ward away, practice you will have a female ticket soldier brother don't be sad! And you have friends! Teacher, leave me to do homework after class, real mushroom cool ~ Boyfriend was you sent off the red card? Look back at this list, you really shot the real en, our journey, is Star (Huo) Chen (gai) Big (Dan) Sea (Shen) Alive a literary single Wang, pack away bear children while cool go ... Uncle and aunt are still alone tomorrow, a person to pay the number of electricityShadow ticket, a person to play repeatedly (kill a pair is a pair), do a quiet beautiful male "," End ": 0," image ": {" key ":" 293634E25FC4C886C957203DC8C9734D "," Name ":" 6a3fecd2-f565-4ff1-ba03-943170e5a301 (1). jpg "," url ":" Http://nos-yx.netease.com/yixinpublic/pr_ 6cntqgf6tlv8yr6eybbbxg==_1447149809_77211016 "}," Start ": 0," SubTitle ":" Breaking!!! On the eve of the dog-abusing festival on Singles Day, there is a list of Tanwali that is full of malice. We look look~ This list single out, each road hero lay gun countless! Men and women have said that they have finally found their single reason! ———— Dead Division line ———— Warrior your melancholy temperament has betrayed you, this evil sword to ward away, practice you will have a female ticket soldier brother don't be sad! And you have friends! Teacher, leave me to do homework after class, real mushroom cool ~ Boyfriend was you sent off the red card? Look back at this list, you really shot the real en, our journey, is Star (Huo) Chen (gai) Big (Dan) Sea (Shen) Alive a literary single Wang, pack away bear children while cool go ... Uncle and aunt are still alone. Tomorrow, a person to buy tickets, a person to play repeatedly see (Kill a pair is a pair), do a quiet beauty male, "Subsubtype": 0, "title": "Wang!" Unlock the mysteries of your single years! "}]}," Subtype ": 101}} ' Parsejson (JSONSTR3) Parsejson (JSONSTR1) Parsejson (JSONSTR2) Parsejson (JSONSTR4)}func Par Sejson (content String) {audio: = Gjson. Get (content, "audio") Location: = Gjson. Get (Content, "location") Text: = Gjson. Get (Content, "text") Images: = Gjson. Get (content, "images") Richtext:=gjson. Get (Content, "Richtext.data.items")/* Richtext:=gjson. Get (Content, "RichText") */if audio. Exists () {duration: = audio. Get ("duration"). String () URL: = audio. Get ("url"). String () Name: = Audio. Get ("name"). String () println (duration, URL, name)} if location. Exists () {coordinate: = location. Get ("coordinate"). String () println ("latitude", coordinate)} if text. Exists () {textstr: = text. String () println (TEXTSTR)} if images. Exists () {Re: = images. Array () for _, V: = Range Re {re: = v.get ("url"). String () println ("Picture url", re)}} if RichText. Exists () {re:= richtext. Array () for _, V: = Range Re {re: = V.get ("desc"). String () re2:=v.get ("Image.url"). String () println ("desc", re) println ("Rich text built-in image url", Re2)}}



Author: Send you a bowl of barley tea
Links: https://www.jianshu.com/p/ef3fcf94295b
Source: Jane book
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.