1

I got a small chunk of code that i need to parse out

I just want the value like

longname (shortname) such as Euro (EUR) or maybe Bristish Pound (GBP)

and output it into a file.txt using C++ in this way

Euro (EUR)
British Pound (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

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 に答える
0

lex と yacc を使用できます。これには、外部ライブラリの依存関係がないという利点があります。

于 2012-07-25T10:31:07.200 に答える