Parsing JSON data from Ethernet


hello!

i've been trying parse json data https://api.coindesk.com/v1/bpi/currentprice/usd.json gather time , current bitcoin value in usd. have been able raw json formatted data web service cannot parse it. have been using https://github.com/interactive-matter/ajson library parse data. here's current code:

code: [select]
#include <jsonarray.h>
#include <jsonhashtable.h>
#include <jsonobjectbase.h>
#include <jsonparser.h>

/*
  dns , dhcp-based web client

circuit:
* ethernet shield attached pins 10, 11, 12, 13
*/

#include <spi.h>
#include <ethernet.h>

// enter mac address controller below.
byte mac[] = {  0x00, 0xaa, 0xbb, 0xcc, 0xde, 0x02 };
char servername[] = "api.coindesk.com";
char json[] = "";

jsonparser<64> parser;

string location = "/~v1/bpi/currentprice/usd.json";

// initialize ethernet client library
ethernetclient client;

void setup() {
  // start serial library:
  serial.begin(9600);
 
  serial.print("arduino ethernet bitcoin json data");
 
 
  // start ethernet connection:
  if (ethernet.begin(mac) == 0) {
    serial.println("failed configure ethernet using dhcp");
    // no point in carrying on, nothing forevermore:
    while(true);
  }
  // give ethernet shield second initialize:
  delay(1000);
  serial.println("connecting...");

  // if connection, report via serial:
 
  if (client.connect(servername, 80)) {
    serial.println("connected");
    // make http request:
    client.println("get https://api.coindesk.com/v1/bpi/currentprice/usd.json");
    client.println();
  }
  else {
    // if didn't connection server:
    serial.println("connection failed");
  }
}

void loop()
{
  // if there incoming bytes available
  // server, read them , print them:
  if (client.available()) {
    char json = client.read();
 
    serial.println();
   
    serial.print("going start parsing: ");
    serial.print(json);
   
    serial.println();
   
    jsonhashtable hashtable = parser.parsehashtable(json);
   
    if (!hashtable.success()){
      serial.print("hash table function failed...");
      return;
    }
    char time = hashtable.getlong("time");
    serial.println();
    serial.print("current time: ");
    serial.print(time);
  }

  // if server's disconnected, stop client:
  if (!client.connected()) {
    serial.println();
    serial.println("disconnecting.");
    client.stop();

    // nothing forevermore:
    while(true);
  }
}


whenever try compile sketch, following error:

code: [select]
arduino_json_ethernet.ino: in function 'void loop()':
arduino_json_ethernet:73: error: invalid conversion 'char' 'char*'
arduino_json_ethernet:73: error: initializing argument 1 of 'jsonhashtable jsonparser<n>::parsehashtable(char*) [with int max_tokens = 64]'


i noticed happens when define same variable "json" in both parser , ethernet function.

any useful in advance!

your code reading 1 character client, , trying parse it. won't work. need read whole json message before can parse it. see examples of buffering messages in code reads serial port, , code reading network, , quite you'll find examples accompanying json library too.


Arduino Forum > Using Arduino > Programming Questions > Parsing JSON data from Ethernet


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support