Read JSON

733
1
Jump to solution
12-10-2021 04:22 PM
Labels (1)
pspada_WT
New Contributor III

Hi,

I'm starting to develop a SOE that take this JSON

{
 "INSERT":[
     {
           "url":"C:\\Users\\pasq4\\Documents\\where_tech\\TELECOM\\PRO\\SDK.gdb\\test_1_1",
          "ids":[4,6]
     },
    {
           "url":"C:\\Users\\pasq4\\Documents\\where_tech\\TELECOM\\PRO\\SDK.gdb\\test_1_1",
           "ids":[4,6]
    }

]
}

 

I've tryed with all the possibility of "operationInput.TryGet...." but i Don't know how to take this json string correctly. 

 

Someone can help me?

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Your JSON contains an "INSERT" JSON object with a JSON array, so you need to parse the JSON object and array first and then get their values inside. Say if you have an input parameter called "parm1" in your REST SOE, and your JSON is the value passed to the "parm1" parameter, you can parse the JSON like this:

            JsonObject result = new JsonObject();
            JsonObject parm1Value;
            bool found = operationInput.TryGetJsonObject("parm1", out parm1Value);
            if (!found)
                throw new ArgumentNullException("parm1");
            object[] insertArr;
            if (!parm1Value.TryGetArray("INSERT", out insertArr))
                return Encoding.UTF8.GetBytes(result.ToJson());
            foreach (object insertContent in insertArr)
            {
                JsonObject insertJson = insertContent as JsonObject;
                if (insertJson != null)
                {
                    string url;
                    object[] ids;
                    if (insertJson.TryGetString("url", out url))
                        System.Diagnostics.Debug.WriteLine(url);
                    if (insertJson.TryGetArray("ids", out ids))
                        System.Diagnostics.Debug.WriteLine(String.Join(",", ids.Cast()));
                }
            } 

 

View solution in original post

0 Kudos
1 Reply
by Anonymous User
Not applicable

Your JSON contains an "INSERT" JSON object with a JSON array, so you need to parse the JSON object and array first and then get their values inside. Say if you have an input parameter called "parm1" in your REST SOE, and your JSON is the value passed to the "parm1" parameter, you can parse the JSON like this:

            JsonObject result = new JsonObject();
            JsonObject parm1Value;
            bool found = operationInput.TryGetJsonObject("parm1", out parm1Value);
            if (!found)
                throw new ArgumentNullException("parm1");
            object[] insertArr;
            if (!parm1Value.TryGetArray("INSERT", out insertArr))
                return Encoding.UTF8.GetBytes(result.ToJson());
            foreach (object insertContent in insertArr)
            {
                JsonObject insertJson = insertContent as JsonObject;
                if (insertJson != null)
                {
                    string url;
                    object[] ids;
                    if (insertJson.TryGetString("url", out url))
                        System.Diagnostics.Debug.WriteLine(url);
                    if (insertJson.TryGetArray("ids", out ids))
                        System.Diagnostics.Debug.WriteLine(String.Join(",", ids.Cast()));
                }
            } 

 

0 Kudos