Hi MArtin,
I have copied the same code from the link and pasted here.Please help me to modify the code that would return the value for the sample response i have attached in my question.
class JsonReader
{
Newtonsoft.Json.Linq.JObject jObject;
}
public void loadJson(str _json)
{
;
jObject = Newtonsoft.Json.Linq.JObject::Parse(_json);
}
public static JsonReader parseJson(str _json)
{
JsonReader reader = new JsonReader();
;
reader.loadJson(_json);
return reader;
}
private anytype traversePath(str path,
Newtonsoft.Json.Linq.JContainer obj = jObject)
{
List pathElements;
ListEnumerator le;
Newtonsoft.Json.Linq.JValue value;
Newtonsoft.Json.Linq.JToken token;
Newtonsoft.Json.Linq.JTokenType thisType,
nestedType;
Newtonsoft.Json.Linq.JObject newObject;
Newtonsoft.Json.Linq.JArray newArray;
str current,
thisTypeString,
nestedTypeString;
#define.JObject("Newtonsoft.Json.Linq.JObject")
#define.JArray ("Newtonsoft.Json.Linq.JArray")
;
pathElements = strSplit(path, @".\/");
le = pathElements.getEnumerator();
if (le.moveNext())
{
current = le.current();
thisType = obj.GetType();
thisTypeString = thisType.ToString();
switch (thisTypeString)
{
case #JObject:
token = obj.get_Item(current);
break;
case #JArray:
token = obj.get_Item(str2int(current) - 1);
break;
default:
return null;
}
if (token)
{
nestedType = token.GetType();
nestedTypeString = nestedType.ToString();
if (nestedTypeString != #JObject && nestedTypeString != #JArray)
{
switch (thisTypeString)
{
case #JArray:
return obj.get_Item(str2int(current) - 1);
case #JObject:
return obj.get_Item(current);
default:
return null;
}
}
switch (nestedTypeString)
{
case #JObject:
newObject = Newtonsoft.Json.Linq.JObject::FromObject(token);
return this.traversePath(strDel(path, 1, strLen(current) 1), newObject);
case #JArray:
newArray = Newtonsoft.Json.Linq.JArray::FromObject(token);
return this.traversePath(strDel(path, 1, strLen(current) 1), newArray);
default:
return null;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
public real getRealNode(str path)
{
return this.traversePath(path);
}
public int getIntNode(str path)
{
return this.traversePath(path);
}
public str getStringNode(str path)
{
return System.Convert::ToString(this.traversePath(path));
}
public boolean isFound(str _path)
{
return this.traversePath(_path) != null;
}
job to retrieve the array of objects
JsonReader reader;
str json,
strResult;
int i,
intResult;
;
json = "{\"object\":[{\"Property\":\"Alpha\",\"Value\":1},{\"Property\":\"Beta\",\"Value\":2},{\"Property\":\"Gamma\",\"Value\":3}]}";
reader = JsonReader::parseJson(json);
for (i = 1; reader.isFound(strfmt("object.%1.Property", i)); i )
{
strResult = reader.getStringNode(strfmt("object.%1.Property", i));
intResult = reader.getIntNode(strfmt("object.%1.Value", i));
info(strfmt("%1 = %2", strResult, intResult));
}
Here I have made slight change to retrieve my first level data that is to return A's value only since my response is
{"data":
{"A":"value1=","B":{"B1":{"ValueB1":[{"c1":"Test c1","c2":"c2 value"}},
"D":{},
"E":{}},
"F":[{"F1":"F1 value","F2 lang":"en"},
"email":"testEmail id",
"tokens":{"G1_token":"test token value"},
"name":"Test name","phone":"123456"}}
//////////////////////////////////
if(reader.isFound(strfmt("data.A", i)))
{
strResult = reader.getStringNode(strfmt("data.A", i));
info(strfmt("%1", strResult));//, intResult));
}
Please let me know how to retrieve the value for other nodes and forloop
and let me know code sample to pass values for the Body data to pass in Json request string and format of my request body data is posted in my question