Select to view content in your preferred language

SOE return Json with 2D array

787
2
10-24-2013 02:31 PM
ReneRubalcava
Honored Contributor
I'm using ArcGIS Server 10.11, .NET SOE.

I'm trying to convert a List<List<int>> to a Json result in our .NET SOE and it keeps throwing the following error.

{
    
 "error": {
        
  "code": 500,
        
  "message": "Object cannot be stored in an array of this type."
 
    }

}


Here is the code I am using.
List<List<int>> results = UtilHelper.RunMethod();
JsonObject json = new JsonObject();
JsonObject[][] jsonResults = new JsonObject[results.Count][];
 
for (int j = 0; j < results.Count; j++)
{
 var list = results.ElementAt(j);
 JsonObject[] obj = new JsonObject[list.Count];
 for (var k = 0; k < list.Count; k++)
 {
  obj.SetValue(list.ElementAt(k), k);
 }
 jsonResults.SetValue(obj, j);
}
 
json.AddArray("results", jsonResults);
 
return Encoding.UTF8.GetBytes(json.ToJson());


The result would look something like
{
  "results":[
    [1,2,3],
    [11,22,33],
    [], // an array could be empty
    [99,100,101]
  ]
}


Eventually, we're going to refactor this out to build JsonObject[] using that helper method, but for now, I just need to convert this so we don't have to update much more on the client side other than the URL endpoints.

I'm a little stumped on this one. If I parse the results using StringBuilder, they come out ok, but then they are not JSON and I can't parse them on the client side.

I can't find much info in the docs for JsonObject and it looks like there is a JSONArray, but I'm unsure how to use that or even if I should.

Any insight would be greatly appreciated, thanks!
0 Kudos
2 Replies
ReneRubalcava
Honored Contributor
Nevermind, I was able to just use http://james.newtonking.com/json

List<List<int>> results = FlowTrace.RunFlowTrace(dto);
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(results));


Even using StringBuilder and it worked.

Thanks.
0 Kudos
JeffJacobson
Frequent Contributor
FYI, another good JSON serializer for .NET is ServiceStack.Text. It is supposed to perform better than Newtonsoft.Json (but I've never tested this myself). Like Newtonsoft's serializer, ServiceStack.Text is also available via NuGet.
0 Kudos