Page 1 of 1

extraer cadenas

Posted: Fri Mar 15, 2013 10:20 pm
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.

Re: extraer cadenas

Posted: Fri Mar 15, 2013 11:48 pm
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

Re: extraer cadenas

Posted: Sat Mar 16, 2013 2:02 am
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

Re: extraer cadenas

Posted: Sat Mar 16, 2013 7:24 am
by wmormar
Daniel Garcia-Gil,

De maestros sin duda..

Re: extraer cadenas

Posted: Sat Mar 16, 2013 9:34 am
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.

Re: extraer cadenas

Posted: Sat Mar 16, 2013 11:59 am
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"]

Re: extraer cadenas

Posted: Sat Mar 16, 2013 3:46 pm
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

Re: extraer cadenas

Posted: Sun Mar 17, 2013 7:49 am
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

Re: extraer cadenas

Posted: Sun Mar 17, 2013 10:06 am
by elvira
Muchas gracias.

Hay que usar la función HB_UTF8TOSTR( cJson )

Has sido Daniel de una ayuda tremenda.

Saludos