Workflow WebRequest response conversion
Hi,
I am currently trying to figure out how to convert a WebRequest response to a managable data representation. So far I haven't been able to find any case study on this, so I'm directing my question to this community. :)
I'm performing a request to a service and getting a response according to the structure in below sample:
[{"Id":1,"Name":"Aartselaar","NisCode":11001},{"Id":2,"Name":"Antwerpen","NisCode":11002},{"Id":3,"Name":"Boechout","NisCode":11004},{"Id":4,"Name":"Boom","NisCode":11005},{"Id":5,"Name":"Borsbeek","NisCode":11007},{"Id":6,"Name":"Brasschaat","NisCode":11008},{"Id":7,"Name":"Brecht","NisCode":11009},{"Id":8,"Name":"Edegem","NisCode":11013}]
What I want to achieve, is to convert this to an enumerable datastructure. So far I haven't been able to solve this jigsaw puzzle.
I have tried performing a series of assignments:
MunicipalitiesAsString = System.Text.Encoding.UTF8.GetString(Municipalities)
MunicipalitiesObject = Jayrock.Json.Conversion.JsonConvert.Import(MunicipalitiesAsString)
MunicipalitiesJSON = CType(MunicipalitiesObject,Jayrock.Json.JsonArray)
MunicipalitiesJSON is not enumerable though, so I can't iterate through it with a foreach loop.
How do I go about this? Or if someone could point me to a guide about using workflows to prepare data from requests for further operations, that would also help me a ton.
kind regards,
Ibrahim
0
-
Hi Ibrahim,
The text you are trying to parse is a JSON array so you'll want to do something like this in an Assign activity:Newtonsoft.Json.Linq.JArray.Parse(MunicipalitiesAsString)This returns an Object, but you can then Cast it to a Newtonsoft.Json.Linq.JArray. The JArray object can be enumerated. http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JArray.htm
There are lots of ways to parse JSON data, but the Netwonsoft library is a great choice.
--Ryan0 -
Minor correction. JArray.Parse(string) does return a JArray so there is no need for the extra cast. http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JArray_Parse.htm
--Ryan0 -
Hi Ryan,
Thank you for the directions ;)
I was not aware of the Newtonsoft library, it seems like it fulfills more than the Jayrock library.
Kind regards,
Ibrahim0 -
@Ryan, I am trying to do basically the same thing - read a string of JSON objects but I keep getting the following error when I run the workflow:
Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself
Any suggestions?
Thanks,
Bryan0 -
@Bryan, were you able to resolve this. I'm running into the same trouble.
Dave0 -
@David Flack, I did figure it out but I can't remember exactly what the issue was because I can't remember which workflow I was working on at the time. Here is the last one I remember having issues with. I got around it by using the JsonToDictionary task then casting it to an array of objects and iterating over the array casting to individual objects. I have attached the workflow so you can see how I did it. It also includes a sample JSON file that you will need to copy to your server and update the DownloadString task to point at the file. Hope that helps.... 0
Du måste logga in om du vill lämna en kommentar.
Kommentarer
6 kommentarer