2

i have a json file which i need to parse and get some string from it is there any code or example in managed c++ (c++/cli) thanks. here is an extract from my json file i nedd to get all Nodes

{ "ID": "{15DFD536-EC23-4624-803E-5AA719DC7A85}", " Nodes": [ -0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0, -0.01, 0.01 ]}

2
  • 1
    Using the Json.NET library is boilerplate, works fine from C++/CLI as well. Commented Jun 18, 2013 at 11:51
  • +1 Json.NET is the way to go, the best JSON library in .Net. :) Commented Jun 18, 2013 at 13:06

1 Answer 1

3

Here is a sample using Json.NET:

#include "stdafx.h"

using namespace System;
using namespace Newtonsoft::Json;

ref class MyData
{
    public: Guid ID;
    public: array<double>^ Nodes;
};

int main(array<System::String ^> ^args)
{
    String^ json = "{ \"ID\": \"{15DFD536-EC23-4624-803E-5AA719DC7A85}\", \"Nodes\": [ -0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0, -0.01, 0.01 ]}";

    MyData^ data = JsonConvert::DeserializeObject<MyData^>(json);

    Console::WriteLine("ID: {0}\nNodes: {1}", data->ID, String::Join(",", System::Linq::Enumerable::Cast<Object^>(data->Nodes)));

    return 0;
}

Result:

ID: 15dfd536-ec23-4624-803e-5aa719dc7a85
Nodes: -0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0,-0.01,0.01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.