2021-06-16 03:49 PM - last edited on 2021-09-14 09:34 AM by Noemi Balogh
JSON::JDOMParser jParser; JSON::ValueRef parsed = jParser.Parse(clientConnection.BeginReceive(response)); JSON::ObjectValueRef obj = GS::DynamicCast<JSON::ObjectValue>(parsed); GS::UIndex idx = 0; GS::UniString name = obj->GetName(idx);//This gets me the value "results" as it should bool isObj = obj->IsObject();//true JSON::ValueRef resultsRef = obj->Get("results");//If I DBPrintf this it ouputs "Array"I don't fully understand how to "navigate" or "read" the parsed Object. I didn't found any documentation about the JSON Parser below
JSON::JDOMParserCan you please help me with some ideas how to traverse this JSON object?
2021-06-17 03:35 PM
using namespace HTTP::Client; using namespace HTTP::MessageHeader; using namespace GS::IBinaryChannelUtilities; JSON::JDOMParser jParser; IO::URI::URI connectionUrl("http://geoportal.ancpi.ro/geoprocessing/rest/services/LOOKUP/UATLookup/GPServer/FastSelect/execute?f=json&Expression=WORKSPACEID=10"); ClientConnection clientConnection(connectionUrl); clientConnection.Connect(); Request getRequest(Method::Get, connectionUrl); getRequest.GetRequestHeaderFieldCollection().Add(HeaderFieldName::UserAgent, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"); clientConnection.Send(getRequest); Response response; //Parse the top object of the JSON File JSON::ValueRef parsed = jParser.Parse(clientConnection.BeginReceive(response)); JSON::ObjectValueRef obj = GS::DynamicCast<JSON::ObjectValue>(parsed); //Get the array named results into arrValRef GS::UniString atName = "results"; JSON::ArrayValueRef arrValRef; obj->GetMandatoryMember(atName, arrValRef); //Get the first and single item of the array JSON::ValueRef first = arrValRef->Get(0); //Parse it again into an ObjectValueRef JSON::ObjectValueRef resultsObj = GS::DynamicCast<JSON::ObjectValue>(first); //debug so I can be sure what it is DBPrintf("Size of reultsObj is %u\n", resultsObj->GetSize()); DBPrintf("Type of resultsObj[2] is %s\n", resultsObj->GetName(2).ToCStr().Get()); //Get the third element from results array : "value" this is an object JSON::ValueRef valuesRef = resultsObj->Get(2); //Parse it into a ObjectvalueRef JSON::ObjectValueRef valuesObj = GS::DynamicCast<JSON::ObjectValue>(valuesRef); //Get the array I wanted finally, the features array containing information about cities JSON::ArrayValueRef fieldsArray; valuesObj->GetMandatoryMember("features", fieldsArray); //Iterate trough the "features" array for (unsigned int i = 0; i <= fieldsArray->GetSize(); i++) { //Debug to be sure that the correct one is selected DBPrintf("Working %u times\n", i); } /*GS::IChannelX channel(clientConnection.BeginReceive(response), GS::GetNetworkByteOrderIProtocolX()); DBPrintf("HTTP %u\n", response.GetStatusCode()); DBPrintf("Received data: \n"); GS::UniString body = ReadUniStringAsUTF8(channel, StringSerializationType::NotTerminated); GS::Array<GS::UniString> lines; body.Split("\n", GS::UniString::KeepEmptyParts, &lines); for (GS::UniString& line : lines) { DBPrintf("%s\n", line.ToCStr(0, MaxUSize, GSCharCode::CC_Legacy).Get()); } DBPrintf("Read %u characters, %u lines \n", body.GetLength(), lines.GetSize());*/ clientConnection.FinishReceive(); clientConnection.Close(false);
2021-06-17 03:50 PM
"attributes": { "OID": 2, "ADMINISTRATIVEUNITID": 1213, "UAT": "Aiud" }The JDOMParser gave me some inconsistent results as shown bellow.
Type of fieldsArray[0] is: Array Type of fieldsArray[1] is: String Type of fieldsArray[2] is: Unknown Type of fieldsArray[3] is: Bool Type of fieldsArray[4] is: Unknown Type of fieldsArray[5] is: Unknown Type of fieldsArray[6] is: String Type of fieldsArray[7] is: Unknown Type of fieldsArray[8] is: Unknown Type of fieldsArray[9] is: Unknown Type of fieldsArray[10] is: Null Type of fieldsArray[11] is: Unknown Type of fieldsArray[12] is: Bool Type of fieldsArray[13] is: Unknown Type of fieldsArray[14] is: Unknown Type of fieldsArray[15] is: Null Type of fieldsArray[16] is: Unknown Type of fieldsArray[17] is: Unknown Type of fieldsArray[18] is: Object Type of fieldsArray[19] is: Array Type of fieldsArray[20] is: Unknown Type of fieldsArray[21] is: Null Type of fieldsArray[22] is: Object Type of fieldsArray[23] is: Unknown Type of fieldsArray[24] is: Bool Type of fieldsArray[25] is: Unknown Type of fieldsArray[26] is: Unknown Type of fieldsArray[27] is: Bool Type of fieldsArray[28] is: Unknown Type of fieldsArray[29] is: Unknown Type of fieldsArray[30] is: Bool Type of fieldsArray[31] is: Unknown Type of fieldsArray[32] is: Unknown Type of fieldsArray[33] is: Bool Type of fieldsArray[34] is: Unknown Type of fieldsArray[35] is: Unknown Type of fieldsArray[36] is: Bool Type of fieldsArray[37] is: Unknown Type of fieldsArray[38] is: Unknown Type of fieldsArray[39] is: Null Type of fieldsArray[40] is: Unknown Type of fieldsArray[41] is: Unknown Type of fieldsArray[42] is: Object Type of fieldsArray[43] is: Unknown Type of fieldsArray[44] is: Unknown Type of fieldsArray[45] is: Null Type of fieldsArray[46] is: Null Type of fieldsArray[47] is: Unknown Type of fieldsArray[48] is: Bool Type of fieldsArray[49] is: Unknown Type of fieldsArray[50] is: Unknown Type of fieldsArray[51] is: Unknown Type of fieldsArray[52] is: Unknown Type of fieldsArray[53] is: Unknown Type of fieldsArray[54] is: Bool Type of fieldsArray[55] is: Array Type of fieldsArray[56] is: Unknown Type of fieldsArray[57] is: Bool Type of fieldsArray[58] is: Object Type of fieldsArray[59] is: Unknown Type of fieldsArray[60] is: Unknown Type of fieldsArray[61] is: Unknown Type of fieldsArray[62] is: Unknown Type of fieldsArray[63] is: Unknown Type of fieldsArray[64] is: Unknown Type of fieldsArray[65] is: Unknown Type of fieldsArray[66] is: Object Type of fieldsArray[67] is: Unknown Type of fieldsArray[68] is: Unknown Type of fieldsArray[69] is: Unknown Type of fieldsArray[70] is: Unknown Type of fieldsArray[71] is: Unknown Type of fieldsArray[72] is: String Type of fieldsArray[73] is: Unknown Type of fieldsArray[74] is: Unknown Type of fieldsArray[75] is: Unknown Type of fieldsArray[76] is: Unknown Type of fieldsArray[77] is: Unknown Type of fieldsArray[78] is: Number
2021-06-17 04:28 PM