Ask a question about JSON

Post

Posted
Rating:
#1 (In Topic #441)
Avatar
Trainee
<IMG src="http://zhuanshun.free.liyunf8888.cn/data/log/%E9%80%89%E5%8C%BA_001.png"> </IMG>


As shown in the figure, how can I get this mame value?

Hope to give someone a :cry:
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Hi yzsdn
Make sure your project has the gb.web component ticked.
This component has the JSON & JSONCollection classes that you will require.

You can find an introduction to JSON Collections Here

Below is a piece of example code that I use to get JSON results for a Book search from Google Books.
It will work much the same way but you will need to change the format to suit the JSON format of the results in your example.
ie:  For Each JSonData["songs"]…

I use a WebView control to get the data by setting the URL field to the search string with the API Key ( assume you have one as well)
Once the data is returned, the WebView.Text will contains the JSON data

Code (gambas)

  1. Private Sub GetSearchResults()
  2.   Dim JSonData As New Collection
  3.   Dim TmpInt As Integer = 0
  4.   Dim TmpJson As Collection
  5.   Dim RowNo As Integer = 0
  6.   Dim TmpStr As String
  7.   Dim TmpAuthor As String[]
  8.  
  9.   JSonData = JSON.Decode(WebView1.Text, True)
  10.   If IsNull(JSonData) Then
  11.     Message("Your search [" & AG.APISearchText & "] returned no data")
  12.   Else
  13.         For Each JSonData["items"]
  14.           Grd_Results.Rows.Count = RowNo + 1
  15.           TmpJson = JSonData["items"][RowNo]["volumeInfo"]
  16.           Grd_Results[RowNo, 0].Text = Str(RowNo + 1)
  17.           Grd_Results[RowNo, 1].Text = JSonData["items"][RowNo]["volumeInfo"]["title"]
  18.           TmpAuthor = JSonData["items"][RowNo]["volumeInfo"]["authors"]
  19.           For Each TmpStr In TmpAuthor
  20.             Grd_Results[RowNo, 2].Text = TmpStr & " "
  21.           Next
  22.           Grd_Results[RowNo, 3].Text = JSonData["items"][RowNo]["volumeInfo"]["publishedDate"]
  23.           Grd_Results[RowNo, 4].Text = JSonData["items"][RowNo]["volumeInfo"]["publisher"]
  24.           Inc RowNo
  25.         Next
  26.  
  27.  
  28.  

Note#
Grd_Results is a standard Gridview control.
AG.APISearchText is a global string value used to pass the search text between forms.

This should give you enough information to decode your JSON results - It may take a bit of trial and error.

Hope this helps.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Here is an partial example of the search results for a book called Gullivers Travels

{
  "kind": "books#volumes",
  "totalItems": 41,
  "items": [
    {
      "kind": "books#volume",
      "id": "9XBtCwAAQBAJ",
      "etag": "EHIKp6yCh4E",
      "selfLink": " https://www.googleapis.com/books/v1/volumes/AAAAAAAAAAAA",
      "volumeInfo": {
        "title": "Gullivers Travels",
        "subtitle": "Class Ix",
        "authors": [
          "Jonathan Swift"
        ],
        "publisher": "Prabhat Prakashan",
        "publishedDate": "1938-01-01",
        "description": "Jonathan Swift’s satirical novel was first published in 1726, yet it is still valid today. Gulliver’s Travels describes the four fantastic voyages of Lemuel Gulliver, a kindly ship’s surgeon. Swift portrays him as an observer, a reporter, and a victim of circumstance. His travels take him to Lilliput where he is a giant observing tiny people. In Brobdingnag, the tables are reversed and he is the tiny person in a land of giants where he is exhibited as a curiosity at markets and fairs. The flying island of Laputa is the scene of his next voyage. The people plan and plot as their country lies in ruins. It is a world of illusion and distorted values. The fourth and final voyage takes him to the home of the Houyhnhnms, gentle horses who rule the land.",
        "industryIdentifiers": [
          {
            "type": "ISBN_13",
            "identifier": "9789351864127"
          },
          {
            "type": "ISBN_10",
            "identifier": "935186412X"
          }
        ],
        "readingModes": {
          "text": true,
          "image": true
        },
        "pageCount": 312,
        "printType": "BOOK",
        "categories": [
          "Comics & Graphic Novels"
        ],
        "maturityRating": "NOT_MATURE",
        "allowAnonLogging": false,
        "contentVersion": "0.2.2.0.preview.3",
        "panelizationSummary": {
          "containsEpubBubbles": false,
          "containsImageBubbles": false
        },
        "imageLinks": {
          "smallThumbnail": "
Image

(Click to enlarge)

",
          "thumbnail": "
Image

(Click to enlarge)

"
        },
        "language": "en",
        "previewLink": "Gullivers Travels (Class Ix): GULLIVERS TRAVELS: Jonathan Swift a Popular ... - Jonathan Swift - Google Books",
        "infoLink": "https://play.google.com/store/books/details?id=9XBtCwAAQBAJ&source=gbs_api",
        "canonicalVolumeLink": "https://play.google.com/store/books/details?id=9XBtCwAAQBAJ"
      },
 …..

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Trainee

Quincunxian said

Here is an partial example of the search results for a book called Gullivers Travels

{
  "kind": "books#volumes",
  "totalItems": 41,
  "items": [
    {
      "kind": "books#volume",
      "id": "9XBtCwAAQBAJ",
      "etag": "EHIKp6yCh4E",
      "selfLink": "https://www.googleapis.com/books/v1/volumes/AAAAAAAAAAAA",
      "volumeInfo": {
        "title": "Gullivers Travels",
        "subtitle": "Class Ix",
        "authors": [
          "Jonathan Swift"
        ],
        "publisher": "Prabhat Prakashan",
        "publishedDate": "1938-01-01",
        "description": "Jonathan Swift’s satirical novel was first published in 1726, yet it is still valid today. Gulliver’s Travels describes the four fantastic voyages of Lemuel Gulliver, a kindly ship’s surgeon. Swift portrays him as an observer, a reporter, and a victim of circumstance. His travels take him to Lilliput where he is a giant observing tiny people. In Brobdingnag, the tables are reversed and he is the tiny person in a land of giants where he is exhibited as a curiosity at markets and fairs. The flying island of Laputa is the scene of his next voyage. The people plan and plot as their country lies in ruins. It is a world of illusion and distorted values. The fourth and final voyage takes him to the home of the Houyhnhnms, gentle horses who rule the land.",
        "industryIdentifiers": [
          {
            "type": "ISBN_13",
            "identifier": "9789351864127"
          },
          {
            "type": "ISBN_10",
            "identifier": "935186412X"
          }
        ],
        "readingModes": {
          "text": true,
          "image": true
        },
        "pageCount": 312,
        "printType": "BOOK",
        "categories": [
          "Comics & Graphic Novels"
        ],
        "maturityRating": "NOT_MATURE",
        "allowAnonLogging": false,
        "contentVersion": "0.2.2.0.preview.3",
        "panelizationSummary": {
          "containsEpubBubbles": false,
          "containsImageBubbles": false
        },
        "imageLinks": {
          "smallThumbnail": "
Image

(Click to enlarge)

",
          "thumbnail": "
Image

(Click to enlarge)

"
        },
        "language": "en",
        "previewLink": "Gullivers Travels (Class Ix): GULLIVERS TRAVELS: Jonathan Swift a Popular ... - Jonathan Swift - Google Books",
        "infoLink": "https://play.google.com/store/books/details?id=9XBtCwAAQBAJ&source=gbs_api",
        "canonicalVolumeLink": "https://play.google.com/store/books/details?id=9XBtCwAAQBAJ"
      },
 …..


I have found a suitable solution, but thank you, I will quote your reference in the future :D
Online now: No Back to the top
1 guest and 0 members have just viewed this.