Replies: 2 comments · 1 reply
-
You did not specify what you tried or the errors you got. I gave ChatGPT the proposed JSON document and asked it to produce DOM and On-Demand (our two APIs) solutions. I told it to assume that the file was named
#include <iostream>
#include <vector>
#include <string>
#include "simdjson.h"
int main() {
simdjson::dom::parser parser;
simdjson::dom::element doc = parser.load("json_file.json");
simdjson::dom::array data = doc["data"].get_array();
for (size_t i = 0; i < data.size(); ++i) {
auto item = data.at(i);
std::string_view title = item["title"];
std::string_view date = item["date"];
std::cout << "Title: " << title << ", Date: " << date << std::endl;
}
return EXIT_SUCCESS;
}
#include <cstdlib>
#include <iostream>
#include <string>
#include "simdjson.h"
int main() {
simdjson::ondemand::parser parser;
simdjson::padded_string json = simdjson::padded_string::load("json_file.json");
simdjson::ondemand::document doc = parser.iterate(json);
for (auto data : doc["data"]) {
std::string_view title = data["title"].get_string();
std::string_view date = data["date"].get_string();
std::cout << "Title: " << title << ", Date: " << date << std::endl;
}
return EXIT_SUCCESS;
} I have not verified these solutions but they look correct. Given that you have not specified the difficulties you are encountering, I cannot go any further. Be aware that On-Demand and DOM are two entirely different APIs. You must pick one or the other. Please read our documentation. We work hard to carefully document the library. Further, if you are encountering difficulties, please be specific. |
Beta Was this translation helpful? Give feedback.
-
I did ran it through Gemini but it keeps giving me buggy code. ChatGPT looks better. I just got it to work though, here is my quick code w/o error checking that's based on one
Thanks for getting back to me |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi ya'll,
Looking for someone to give me a hand to parse this JSON string, I've spent hours trying different things but
keep getting errors from simdjson.
Just need a tip in what order things should be done, like define a parser, create a doc from parser, etc. I need to
extract "title" and "date" from each element of an array.
https://pastebin.com/5LEBpmfp
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions