1

我有一小段代码需要解析

我只想要这样的价值

长名(短名),例如欧元 (EUR) 或英镑 (GBP)

并以这种方式使用C++将其输出到file.txt中

欧元 (EUR)
英镑 (GBP)

{
        "shortname": "EUR",
        "longname": "Euro",
        "users":  "Austria,Belgium,Cyprus,Finland,Helsinki,France,Paris,Germany,Berlin,Greece,Athens,Ireland,Dublin,Italy,Rome,Milan,Pisa,Luxembourg,Malta,Netherlands,Portugal,Sl$
                "alternatives": "ewro,evro",
        "symbol": "€",
        "highlight": "1"
    },
    {
        "shortname": "GBP",
        "longname": "British Pound",
        "users":  "United Kingdom,UK,England,Britain,Great Britain,Northern Ireland,Wales,Scotland,UK,Isle of Man,Jersey,Guernsey,Tristan da Cunha,South Georgia and the South San$
        "alternatives": "Quid,Pound Sterling,Sterling,London,Cardiff,Edinburgh,Belfast",
        "symbol": "£",
        "highlight": "1"
    },
4

3 回答 3

2

它是 JSON。你最好使用解析器。我建议你jsonCpp链接

于 2012-07-25T09:36:18.913 回答
0

您可以使用 lex 和 yacc。这样做的好处是没有外部库依赖。

于 2012-07-25T10:31:07.200 回答
0

我建议你http://www.boost.org/doc/libs/1_50_0/doc/html/property_tree.html

用法示例。

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <sstream>
#include <iostream>

int main()
{
   using namespace boost::property_tree;
   std::string s = "{\
        \"shortname\": \"EUR\",\
        \"longname\": \"Euro\",\
        \"users\":  \"Austria,Belgium,Cyprus,Finland,Helsinki,France,Paris,Germany,\
        Berlin,Greece,Athens,Ireland,Dublin,Italy,Rome,Milan,Pisa,Luxembourg,Malta,Netherlands,Portugal\",\
        \"alternatives\": \"ewro,evro\",\
        \"symbol\": \"€\",\
        \"highlight\": \"1\"\
    }";
    std::stringstream ss(s);
    ptree pt;
    json_parser::read_json(ss, pt);
    std::string short_n = pt.get<std::string>("shortname");
    std::string long_n = pt.get<std::string>("longname");
    std::cout << long_n << "(" << short_n << ")" << std::endl;
}

http://liveworkspace.org/code/7b9bf87f128a2fe42d606305f4411771

于 2012-07-25T15:46:52.730 回答