Consuming a Json file returning from rest web service

Could someone send me an example of how to use json_parser to read a Json file? I will need to consume a Rest web service developed in Rails. This web service will respond to my code using json, in other words, from my C++ code running over linux I will access an url which will return to me a file in Json format.
closed account (3hM2Nwbp)
By json_parser do you mean the one from boost? This is from a past project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef JSON_READER_HPP_INCLUDED
#define JSON_READER_HPP_INCLUDED

#include <string>

#include <boost/noncopyable.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>


/**
 *	@class
 *		JSONReader
 *
 *	@brief
 *		A class for reading JSON files
 *
 *	@details
 *		A convenience class to encapsulate a property tree (preventing a "dead tree" from ever being created)
 *		"Dead Tree" - A property tree that has not yet been fed a file.
 *
 *	@see
 *		boost::property_tree
 *		boost::noncopyable
 */
class JSONReader : private boost::noncopyable
{

	///The underlying ptree
	boost::property_tree::ptree tree_;

	public:
	
		/// Constructs a JSONReader
		/**
		 *	@pre
		 *		The specified file must exist
		 *	@post
		 *		This JSONReader now contains the parsed file
		 *	@throw
		 *		This constructor may throw boost::property_tree::ptree_error if a parsing error occurs
		 *	@note
		 *		This constructor makes the strong exception guarantee
		 */
		JSONReader(const std::string& fileName)
		{
			boost::property_tree::read_json(fileName, tree_);
		}

		/// Attempts to read the value of the provided key
		/**
		 *	@pre
		 *		The specified key must exist
		 *	@post
		 *		None
		 *	@param key
		 *		The key to read
		 *	@return
		 *		The value read from the provided key
		 *	@throw
		 *		This method may throw boost::property_tree::ptree_error if the data cannot be converted to the template type
		 *		or if the provided key does not exist
		 *	@note
		 *		This method makes the strong exception guarantee
		 */
		template<typename T>
		T read(const std::string& key)
		{
			return this->tree_.get<T>(key);
		}

		/// Destroys an JSONReader
		/**
		 *	@pre
		 *		None
		 *	@post
		 *		None
		 *	@note
		 *		This destructor makes the no-throw guarantee
		 */
		~JSONReader(void)
		{
		
		}


};
#endif

int main()
{
	JSONReader reader("test.json");
	reader.read<std::string>("TEST");
	return 0;
}


Pardon how the tabs line up, but real men use tabs. :P

*Edit - updated documentation, I modified the original code a bit.
Last edited on
Luc Lieber, first of all, thanks to answer my question. In this example, we assume that we already have the file downloaded to local folder. How do I access a rest service like this http://my_gienah:3000/Tipoestruturas/ObtemTipoEstrutura_IdDominio/1.json from C++ using boost over linux? I am thinking about boost, but if you suggest other library, I will appreciate, for example, Curl. My big difficult is: I am new in C++ and I have short time to do a small but efficient program to consume rest services. The return will be something like this:

[{"DscTipoEstrutura":"Unidade de Neg\u00f3cio","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":1,"NroOrdem":0},{"DscTipoEstrutura":"Regi\u00e3o","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":2,"NroOrdem":1},{"DscTipoEstrutura":"Distrito","FlgAtivo":true,"IdDominio":1,"IdTipoEstrutura":3,"NroOrdem":2}]
closed account (3hM2Nwbp)
I'm not exactly sure what a "rest service" is, but if you have the JSON file already downloaded to a local folder, then you can just use the class posted above (after installing boost) to read it. I don't have any experience with the Curl library, so if you need to do anything with the JSON file beyond reading it into your C++ application, then I can't be of further use, sorry.
Topic archived. No new replies allowed.