Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in understanding the example #8

Closed
mrohner opened this issue Jan 22, 2017 · 5 comments
Closed

Problem in understanding the example #8

mrohner opened this issue Jan 22, 2017 · 5 comments

Comments

@mrohner
Copy link

mrohner commented Jan 22, 2017

This is not about an issue per se but lack of my understanding the example. May be you can give me a hint on how to proceed.
This is the link to the json I'm trying to parse:
http://online.fahrplan.zvv.ch/bin/stboard.exe/dny?input=Z%C3%BCrich,+Oerlikon+(SBB)&dirInput=Z%C3%BCrich+HB&maxJourneys=6&boardType=dep&start=1&tpl=stbResult2json

How do I translate this to the structure given in the example:
char json[] = "{"a":3, "b":{"c":"d"}}";

Any idea?

@squix78
Copy link
Owner

squix78 commented Jan 23, 2017

Hi
I'm currently working on a blog post which will be published this Friday about how to use the parser. You can see the draft in the attachment. Let me know if that helped or what is unclear...[
ESP8266_ The JSON Streaming Parser _ Squix - TechBlog.pdf
](url)

@mrohner
Copy link
Author

mrohner commented Jan 23, 2017 via email

@squix78
Copy link
Owner

squix78 commented Jan 23, 2017

I will answer in English, then more people can profit: if your keys are repeating you'll have to remember your context. You could for instance remember the name of the parent of your current object. You do this in the startObject method and remove it in the endObject method. Have a look here:
https://github.com/squix78/esp8266-weather-station/blob/master/WundergroundClient.cpp#L195
https://github.com/squix78/esp8266-weather-station/blob/master/WundergroundClient.cpp#L494

The second countdown key/value pair for instance has the parent "realtime". So if you were interested in realtime.countdown value you would check if(currentParent == "realtime" && currentKey == "countdown")

If that doesn't help tell me which attributes you need and I will try to hack together the class for you...

@mrohner
Copy link
Author

mrohner commented Jan 30, 2017

Hi,
I gave it a try and even though I still do not fully understand the logic I achieved some promising results.
Nevertheless some questions came up (above JSON data is parsed)
I get values for "type" and "line" but NOT for the two countdown fields. Any ideas?
Is it ok to 'for' loop to capture ten incidents?
Is the parent key retained (e.g. "mainLocation") when a subsequent parent (e.g. "location") overwrites the CurrentParent value? The countdown field is still in "mainLocation" but currentParent has already changed to "location"?
Thank you

void ZVVClient::value(String value) {
for (int i = 0; i < 9 ; i++) { //Number_Journeys
if (currentParent == "product") {
if (currentKey == "type") {
type[i] = value.toInt();
}
if (currentKey == "line") {
line[i] = value.toInt();
}
}
if (currentKey == "countdown") {
a_countdown[i] = value.toInt();
}
if (currentParent == "realTime" && currentKey == "countdown") {
countdown[i] = value.toInt();
}
}
}

@mrohner
Copy link
Author

mrohner commented Jan 31, 2017

I found a solution myself by tracking all the hierarchy levels of the object. Thanks
void ZVVClient::value(String value) {
//Serial.print(currentParent[hierarchy_level]);
//Serial.print(" ");
//Serial.println(currentKey);
if (currentParent[hierarchy_level] == "product") { // Has a Parent key and 2 sub-keys
if (currentKey == "type") {
count++;
Serial.println(count);
type[count] = value.toInt();
//Serial.println(type[count]);
}
if (currentKey == "line") {
line[count] = value.toInt();
//Serial.println(line[count]);
}
}
if (currentParent[hierarchy_level] == "mainLocation" && currentKey == "countdown") {
a_countdown[count] = value.toInt();
//Serial.println(a_countdown[count]);
}
if (currentParent[hierarchy_level-1] == "mainLocation" && currentParent[hierarchy_level] == "realTime" && currentKey == "countdown") {
countdown[count] = value.toInt();
//Serial.println(countdown[count]);
}
}

void ZVVClient::endArray() {
}

void ZVVClient::startObject() {
hierarchy_level++;
currentParent[hierarchy_level] = currentKey;
}

void ZVVClient::endObject() {
hierarchy_level--;
}

@mrohner mrohner closed this as completed Jan 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants