2.2.7.3.1 JSON::FromString


Version

Minimum Origin Version Required: Origin 2017 SR0

Description

Parse a string as JSON and transform it to a struct/class object

Syntax

BOOL FromString( ClassObject data, LPCSTR str, DWORD dwJSONLOAD = 0 )
BOOL FromString( vectorbase & data, LPCSTR str )

Parameters

data
[output]Reference to a struct/class object
str
[input]Stringified JSON string from given object
dwJSONLOAD
[input]Additional options for loading JSON string
JSONLOAD_ODE_SHORTENED = 0x00010000
JSONLOAD_ODE_PREPARE_FOR_IMPORT = 0x00020000
data
[output]vector to accept Javascript array
str
[input]Stringified JSON string from given object

Return

TRUE on success, FALSE on failure

Examples

EX1

void FromString_ex()
{
    string strJSON = "{\"left\":12,\"right\":34,\"top\":56,\"bottom\":78}";
    RECT rect;
    JSON.FromString(rect, strJSON);

    Tree tr;
    if(JSON.FromString(tr, strJSON))
    	out_tree(tr);
    else
    	out_str("Err");
}

EX2

void TestJSON_vector()
{
	vector<string> vs1;
	vs1.Add("hello1");
	vs1.Add("hello2");
	vs1.Add("hello3");
	string strvs1;
	OCJSONToString param;
	param.dwFlags = JSONToStringFlagsDisableIndentation;
	JSON.ToString(vs1, strvs1, &param);
	string strvd1;
	vector<double> vd1;
	vd1.Add(3.14);
	vd1.Add(1024);
	vd1.Add(-3687.3645);
	JSON.ToString(vd1, strvd1, &param);
	
	string strvs2 = "[\"hello1\",\"hello2\",\"hello3\"]";
	vector<string> vs2;
	JSON.FromString(vs2, strvs2);
	string strvd2 = "[3.1400000000000001,1024,-3687.3645000000001]";
	vector<double> vd2;
	JSON.FromString(vd2, strvd2);
	if (strvs1 != strvs2)
		printf("strvs1 != strvs2");
	if (strvd1 != strvd2)
		printf("strvd1 != strvd2");
	
	Worksheet wks;
	wks.Create("Origin");
	Dataset dsA(wks, 0);
	string strds1 = "[314.56, -963, 6317, 8]";
	JSON.FromString(dsA, strds1);
	string strds2;
	JSON.ToString(dsA, strds2);
}

Remark

See Also

JSON::ToString
okutil_http_get

Header to Included

origin.h