Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

How to get array on top of valid JSON #32

Closed
phlegx opened this issue Mar 25, 2014 · 5 comments
Closed

How to get array on top of valid JSON #32

phlegx opened this issue Mar 25, 2014 · 5 comments

Comments

@phlegx
Copy link

phlegx commented Mar 25, 2014

This is valid json data. But how can I get "var1" or "var2"?

  std::string json_str(
      "[ "
      "     \"var1\", "
      "     { \"id\" : \"1234\" }, "
      "     \"var2\", "
      "     \"var3\" "
      "] "
  );
@r-lyeh-archived
Copy link
Contributor

arr.get<jsonxx::String>(0) and arr.get<jsonxx::String>(2) should do the work
be sure that arr is a jsonxx::Array type

@phlegx
Copy link
Author

phlegx commented Apr 2, 2014

Ok, but how can i parse it? I get a string like in my first comment.

Object o;
o.parse(json));

And now?

o.get<jsonxx::String>(0);

@r-lyeh-archived
Copy link
Contributor

use "Array o;" instead. the json you are providing is not an Object, rather than an Array.

@phlegx
Copy link
Author

phlegx commented Apr 2, 2014

Thank you man! :D

#include <iostream>
#include <jsonxx/jsonxx.h>

using namespace std;
using namespace jsonxx;

int main() {
  std::string json_str(
      "[ "
      "     \"var1\", "
      "     { \"id\" : \"1234\" }, "
      "     \"var2\", "
      "     \"var3\" "
      "] "
  );

  Array json;
  json.parse(json_str);
  cout << json.get<String>(0) << endl;

  return 0;
}

@phlegx phlegx closed this as completed Apr 2, 2014
@r-lyeh-archived
Copy link
Contributor

: )

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants