Let's say the GET
response is a flat string containing a large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte, and instead of storing it, just find the key
you need and extract its value
.
HTTPClient http;
http.begin("http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
Serial.println(getValue(http, "97.5 FM", 3, 4));
HTTPClient http;
http.begin("http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
Serial.println(getValue(http, "97.5 FM", 3, 4));
whereWhere the getValue()
function is defined as follows:
String getValue(HTTPClient &http, String key, int skip, int get) {
bool found = false, look = false;
int ind = 0;
String ret_str = "";
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (skip == 0) {
ret_str += char_buff[0];
get --;
} else
skip --;
if (get <= 0)
break;
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
return ret_str;
}
String getValue(HTTPClient &http, String key, int skip, int get) {
bool found = false, look = false;
int ind = 0;
String ret_str = "";
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (skip == 0) {
ret_str += char_buff[0];
get --;
} else
skip --;
if (get <= 0)
break;
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
return ret_str;
}
There is probably (i.e., most likely) a better way of doing this, but this approach works and it's pretty fast, so...
Let's say the GET
response is a flat string containing large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte and instead of storing it just find the key
you need and extract its value
.
HTTPClient http;
http.begin("http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
Serial.println(getValue(http, "97.5 FM", 3, 4));
where the getValue()
function is defined as follows:
String getValue(HTTPClient &http, String key, int skip, int get) {
bool found = false, look = false;
int ind = 0;
String ret_str = "";
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (skip == 0) {
ret_str += char_buff[0];
get --;
} else
skip --;
if (get <= 0)
break;
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
return ret_str;
}
There is probably (i.e., most likely) a better way of doing this, but this approach works and it's pretty fast, so...
Let's say the GET
response is a flat string containing a large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte, and instead of storing it, just find the key
you need and extract its value
.
HTTPClient http;
http.begin("http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
Serial.println(getValue(http, "97.5 FM", 3, 4));
Where the getValue()
function is defined as follows:
String getValue(HTTPClient &http, String key, int skip, int get) {
bool found = false, look = false;
int ind = 0;
String ret_str = "";
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (skip == 0) {
ret_str += char_buff[0];
get --;
} else
skip --;
if (get <= 0)
break;
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
return ret_str;
}
There is probably (i.e., most likely) a better way of doing this, but this approach works and it's pretty fast.
Let's say the GET
response is a flat string containing large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte and instead foof storing it just find whateverthe key
you need and extract its value
.
Let's say the GET
response is a flat string containing large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte and instead fo storing it just find whatever key
you need and extract its value
.
Let's say the GET
response is a flat string containing large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte and instead of storing it just find the key
you need and extract its value
.
Ok, so I was able to do this. Here's the workaround.
Let's say the GET
response is a flat string containing large .json
file with all the commas and curly brackets. The idea is to read it byte-by-byte and instead fo storing it just find whatever key
you need and extract its value
.
We want to find the field containing, say, "97.5 FM" from http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json
(that's at the very end), skip 3 bytes (which is just " | ") and then read and Serial.print
4 bytes (which is "1280")*. We would do something like this (of course make sure to connect to wifi and include <ESP8266HTTPClient.h>
:
HTTPClient http;
http.begin("http://data.nba.net/data/10s/prod/v1/20190223/scoreboard.json");
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
Serial.println(getValue(http, "97.5 FM", 3, 4));
where the getValue()
function is defined as follows:
String getValue(HTTPClient &http, String key, int skip, int get) {
bool found = false, look = false;
int ind = 0;
String ret_str = "";
int len = http.getSize();
char char_buff[1];
WiFiClient * stream = http.getStreamPtr();
while (http.connected() && (len > 0 || len == -1)) {
size_t size = stream->available();
if (size) {
int c = stream->readBytes(char_buff, ((size > sizeof(char_buff)) ? sizeof(char_buff) : size));
if (len > 0)
len -= c;
if (found) {
if (skip == 0) {
ret_str += char_buff[0];
get --;
} else
skip --;
if (get <= 0)
break;
}
else if ((!look) && (char_buff[0] == key[0])) {
look = true;
ind = 1;
} else if (look && (char_buff[0] == key[ind])) {
ind ++;
if (ind == key.length()) found = true;
} else if (look && (char_buff[0] != key[ind])) {
ind = 0;
look = false;
}
}
}
return ret_str;
}
You can call this getValue()
whatever many times you like, but the strings you're looking for need to be only after each other, because the response to GET
is read byte-by-byte just once.
There is probably (i.e., most likely) a better way of doing this, but this approach works and it's pretty fast, so...
* This, of course, is just an example, and you can overload this getValue()
function definition to, say, read until it gets a curly bracket or whatever.