extraer cadenas

Post Reply
elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

extraer cadenas

Post by elvira »

Hola Amigos,

Tengo un fichero como sigue:

Code: Select all

{
 "kind": "books#volumes",
 "totalItems": 1,
 "items": [
  {
   "kind": "books#volume",
   "id": "Vm85LgEACAAJ",
   "etag": "dQePHGu5guw",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/Vm85LgEACAAJ",
   "volumeInfo": {
    "title": "La cura",
    "industryIdentifiers": [
     {
      "type": "ISBN_10",
      "identifier": "8499895328"
     },
     {
      "type": "ISBN_13",
      "identifier": "9788499895321"
     }
    ],
    "pageCount": 432,
    "printType": "BOOK",
    "contentVersion": "preview-1.0.0",
    "language": "en",
    "previewLink": "http://books.google.es/books?id=Vm85LgEACAAJ&dq=isbn:9788499895321&hl=&cd=1&source=gbs_api",
    "infoLink": "http://books.google.es/books?id=Vm85LgEACAAJ&dq=isbn:9788499895321&hl=&source=gbs_api",
    "canonicalVolumeLink": "http://books.google.es/books/about/La_cura.html?hl=&id=Vm85LgEACAAJ"
   },
   "saleInfo": {
    "country": "ES",
    "saleability": "NOT_FOR_SALE",
    "isEbook": false
   },
   "accessInfo": {
    "country": "ES",
    "viewability": "NO_PAGES",
    "embeddable": false,
    "publicDomain": false,
    "textToSpeechPermission": "ALLOWED",
    "epub": {
     "isAvailable": false
    },
    "pdf": {
     "isAvailable": false
    },
    "webReaderLink": "http://books.google.es/books/reader?id=Vm85LgEACAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api",
    "accessViewStatus": "NONE"
   }
  }
 ]
}
 


Necesito por ejemplo obtener el contenido de title, pageCount, etc que tienen una longitud variable.

¿Hay alguna función en Harbour que permita eso?.

Muchas gracias.
User avatar
wmormar
Posts: 1050
Joined: Fri Oct 07, 2005 10:41 pm
Location: México
Contact:

Re: extraer cadenas

Post by wmormar »

elvira,

Podrias hacerle un recorrido con el ttxtfile.

Ejemplo

oTxt := ttxtfile( "miarchivo.txt" )

WHILE !oTxt:eof()
if '"title":' $ oTxt:cLine
cTitle := substr(oTxt:cLine, at(":", oTxt:cLine))
elseif '"pageCount":' $ oTxt:cLine
nPagCount := val(substr(oTxt:cLine, at(":", oTxt:cLine)))
endif
oTxt:skip()
ENDDO

Es al vuelo, esperemos te de una idea
William, Morales
Saludos

méxico.sureste
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: extraer cadenas

Post by Daniel Garcia-Gil »

Hola

harbour cuenta con soporte nativo para Json aqui tienes un ejemplo

el json que tienes lo inclui en un archivo llamado json.txt, la idea es tenerlo accesible como texto
esta compilado con harbour directo sin usar ninguna lib de fivewin

hbmk2 demo.prg

Code: Select all

function main()
    local cJson := memoread("json.txt")
    local hJson
    local item
    hb_jsondecode( cJson, @hJson )

    for each item in hJson["items"]
        ? "title: ", item["volumeInfo"]["title"]
        ? "pageCount: ", item["volumeInfo"]["pageCount"]
    end
return nil
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
User avatar
wmormar
Posts: 1050
Joined: Fri Oct 07, 2005 10:41 pm
Location: México
Contact:

Re: extraer cadenas

Post by wmormar »

Daniel Garcia-Gil,

De maestros sin duda..
William, Morales
Saludos

méxico.sureste
elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

Re: extraer cadenas

Post by elvira »

Queridos Amigos,

Muchas gracias a ambos!!.

No sabía lo del soporte JSON.

No obstante, en este archivo no tengo items como en el anterior, ¿cómo entones puedo por favor hacer el bucle FOR IN EACH?.

Code: Select all

{
 "kind": "books#volume",
 "id": "SFO2lwEACAAJ",
 "etag": "a9baKSHSK9o",
 "selfLink": "https://www.googleapis.com/books/v1/volumes/SFO2lwEACAAJ",
 "volumeInfo": {
  "title": "CODIGO PENAL CON JURISPRUDENCIA",
  "authors": [
   "Morales García"
  ],
  "publisher": "Aranzadi",
  "publishedDate": "2012",
  "industryIdentifiers": [
   {
    "type": "ISBN_10",
    "identifier": "8490142475"
   },
   {
    "type": "ISBN_13",
    "identifier": "9788490142479"
   }
  ],
  "pageCount": 2500,
  "printType": "BOOK",
  "categories": [
   "Education / General"
  ],
  "contentVersion": "preview-1.0.0",
  "language": "es",
  "previewLink": "http://books.google.es/books?id=SFO2lwEACAAJ&hl=&source=gbs_api",
  "infoLink": "http://books.google.es/books?id=SFO2lwEACAAJ&hl=&source=gbs_api",
  "canonicalVolumeLink": "http://books.google.es/books/about/CODIGO_PENAL_CON_JURISPRUDENCIA.html?hl=&id=SFO2lwEACAAJ"
 },
 "saleInfo": {
  "country": "ES",
  "saleability": "NOT_FOR_SALE",
  "isEbook": false
 },
 "accessInfo": {
  "country": "ES",
  "viewability": "NO_PAGES",
  "embeddable": false,
  "publicDomain": false,
  "textToSpeechPermission": "ALLOWED",
  "epub": {
   "isAvailable": false
  },
  "pdf": {
   "isAvailable": false
  },
  "webReaderLink": "http://books.google.es/books/reader?id=SFO2lwEACAAJ&hl=&printsec=frontcover&output=reader&source=gbs_api",
  "accessViewStatus": "NONE"
 }
}
 
Saludos muy cordiales.
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: extraer cadenas

Post by Daniel Garcia-Gil »

Hola...

entonces no necesitas hacerlo...
en el ejemplo anterior lo que quise fue generalizar el proceso con FOR EACH, por si podrian venir mas datos en "items" en futuros JSON que pudieses leer

Json simplemente es un hash, podemos ver en el primer ejemplo que el contenido de "items" es un arreglo, cuyo primer elemento tambien es un hash

Code: Select all

"items": [
  {
   "kind": "books#volume",
   "id": "Vm85LgEACAAJ",
   "etag": "dQePHGu5guw",
   "selfLink": "https://www.googleapis.com/books/v1/volumes/Vm85LgEACAAJ",
   "volumeInfo": {
    "title": "La cura",
...
en una sola linea seria, sin el FOR EACH

Code: Select all

hJson["items"][1]["volumeInfo"]["title"]
como ahora no existe ese arreglo asumo quedaria asi

Code: Select all

hJson["volumeInfo"]["title"]
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

Re: extraer cadenas

Post by elvira »

Daniel,

Un millón de gracias, funciona fenomenal.

Una última cosa, los acentos se ven así:
Image

¿Sabes por qué puede ser?.

He probado con AnsiToOEm y OemToAnsi y nada, se ven mal.

Salu2
User avatar
Daniel Garcia-Gil
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita
Contact:

Re: extraer cadenas

Post by Daniel Garcia-Gil »

Prueba con esto

Code: Select all

#include "fivewin.ch"

REQUEST HB_Lang_ES
REQUEST HB_CODEPAGE_ESWIN


function main()

    local cJson := memoread("json.txt")
    local hJson
    local item
    HB_LangSelect("ES")
    HB_CDPSELECT("ESWIN")
    hb_jsondecode( cJson, @hJson )

    ? "autor: ", hJson["volumeInfo"]["authors"][1]

return nil
our best documentation is the source code
Isla de Margarita Venezuela.
danielgarciagil@gmail.com
http://tdolphin.blogspot.com/
https://www.dropbox.com/referrals/NTI5N ... rc=global9
elvira
Posts: 462
Joined: Fri Jun 29, 2012 12:49 pm

Re: extraer cadenas

Post by elvira »

Muchas gracias.

Hay que usar la función HB_UTF8TOSTR( cJson )

Has sido Daniel de una ayuda tremenda.

Saludos
Post Reply