2

JSONファイルの読み書き初心者です。作成された JSON を読み取った後、std::vector<std::vector<Point>>それぞれに個別にアクセスできるように、(C++ データ型) を JSON ファイルに書き込むにはどうすればよいですか? std::vector<point>助けてください。

4

3 に答える 3

3

POD データ構造を想定すると、次の方法でシリアル化できます。

struct Point
{
    double x, y;
};

Point p;
p.x = 123;
p.y = 456;

std::vector<Point> arr;
arr.push_back(p);

std::vector<std::vector<Point>> container;
container.push_back(arr);
container.push_back(arr);

Json::Value root(Json::arrayValue);
for (size_t i = 0; i != container.size(); i++)
{
    Json::Value temp(Json::arrayValue);

    for (size_t j = 0; j != container[i].size(); j++)
    {
        Json::Value obj(Json::objectValue); 
        obj["x"] = container[i][j].x;
        obj["y"] = container[i][j].y;
        temp.append(obj);
    }

    root.append(temp);
}

結果の JSON は次のとおりです。

[
   [
      {
         "x" : 123.0,
         "y" : 456.0
      }
   ],
   [
      {
         "x" : 123.0,
         "y" : 456.0
      }
   ]
]

C++ とほとんど同じように、配列の配列としてアクセスできます。

于 2014-07-08T14:39:42.300 に答える
0

JSON は次のようになります。

{
  "vectorVectors" : [
    { 
      "pointVectors" : [
        { "point": "<some representation of a point>" },
        // ...
      ]
    },
    // ... 
  ]
}

thatはs のvectorVectorsリストでVectorsあり、 (複数の s を含む)その個々の要素であり、 a の実際の表現です。Vector<Point>pointVectorsVectorVector<Point>pointPoint

このレイアウトを生成する方法については、Rapidjsonを使用できるように見えますが、よくわかりません。使用したことはありません。

于 2014-07-08T14:41:55.587 に答える