getJSON array parameter data in ashx - c#

I'm having problems passing an array parameter to my .ashx handler. Querystring parameters work fine, but I don't know how to access the array from within the ashx file.
My array is a list of strings called list. This is how I call the getJSON method:
jQuery.getJSON('Handlers/AccessoryUpdateHandler.ashx?action=updateselection&a=' + a_sku, function (list) {
//...
});
In AccessoryUpdateHandler.ashx I can get the querystring parameter using context.Request.QueryString["a"];
But how do I get the array data? I've looked at the Request.QueryString object but there's nothing there that looks like my array.
If there's a better way of doing this I'd also be interested to know.

Perhaps you shouldn't rely on QueryString to pass the kind of data. I would advice you to use "POST" method instead. In either case, using the approach you are using, you wont be able to pass the data as an array. Convert the array into a string. A utility like stringify() can come in handy here if you dont want to convert the array into a string manually. When you extract the string from the Request.QueryString or Request.Form (in case you use post) in the handler, you can easily use JavaScriptSerializer to deserialize using a simple C# class that has a schema similar to your json array.
Edited: Illustration
An array like:
var ary = [{ "id": "1", "name": "Mark" }, { "id": "2", "name": "John"}];
can be converted to a string like:
var aryS = "[{\"id\":\"1\",\"name\":\"Mark\"},{\"id\":\"1\",\"name\":\"Mark\"}]";
then, your QueryString can be constructed as:
jQuery.getJSON('Handlers/AccessoryUpdateHandler.ashx?action=updateselection&a=' + aryS, function (list) {
//...
});
when you go to your handler code, you can have a simple class to help you deserialize the data:
class Rec
{
public int Id { get; set; }
public string Name { get; set; }
}
with that in place, the last step to deserialize is:
string s = context.Request.QueryString["a"]; // HttpContext context
var jserializer = new JavaScriptSerializer();
var ary = jserializer.Deserialize<Rec[]>(s);
Note you have to use Rec[] in the Deserialize function since you are deserializing into an array. ary will be an array of Rec with all your data from your javascript array. You need to reference System.Web.Extensions to use JavaScriptSerializer
EDIT:
I thought you were talking about an array of objects. If its just an array of *long*s, the following lines should be sufficient:
string s = context.Request.QueryString["a"]; // HttpContext context
var jserializer = new JavaScriptSerializer();
var ary = jserializer.Deserialize<long[]>(s);
and you dont need to create a class. you would only need that if you had a array of json objects

I don't think your need for passing arrays should dictate whether you use get or post as others suggest.
Using get, the most idiomatic ways is using the same parameter multiple times. e.g.
Handlers/AccessoryUpdateHandler.ashx?a=1&a=2&a=55
In fact in most frameworks(.NET being the exception) when you get a parameter from url you get a array return(often with one item)
My suggestion is you parse context.Request.RawUrl

Related

How to Pass a JSON Values to Nunit Testcase Data

I am reading a JSON file and stores the values as a object
Car : Name, Cost, Yearmodel
Now I need to pass these 3 JSON object values to this Testcasedata.
Likewise I need to pass multiple values while reading the JSON file data.
Any idea explaining on how to do this?
public string JSONParser()
{
StreamReader r = new StreamReader("ESAggregationQuery.json");
string jsonString = r.ReadToEnd();
m = JsonConvert.DeserializeObject<AggModel>(jsonString);
carName = m.carName;
costPrice = m.costPrice;
modelYear = m.modelYear;
return "1";
}
private static IEnumerable<TestCaseData> ESAggregativeTestData
{
get
{
yield return new TestCaseData[] { m.carName, m.costPrice, m.modelYear };
}
}
```
You don't "pass" data to your TestCaseData item. The data has to originate there. Try making the following changes...
Call your JSONParser from the getter of ESAggregativeTestData or add the code in there.
Change your yield statement to return a new TestCaseData, not an array. That's not an NUnit thing... it's just how IEnumerable works. If your JSON file, now or in the future, contains multiple test items, you will need to put all this in a loop.
You have not shown us your test code, which uses the data source. To work with the source you have given, it should take three arguments of the appropriate type.
Suggestion... next time provide more complete source code that shows what you are trying to do.

C# JSON; Deserialize array that sometimes has an empty value (not null)

I'm trying to deserialize a JSON response and I want a function that detects if the array is empty and if it is not empty I want it to return the values.
The JSON array looks like this when empty:
{"metrics":[]}
And when it is not empty it can look like any of the below:
{"metrics":["flow"]}
{"metrics":["energy"]}
{"metrics":["flow","energy"]}
How can I detect this?
It does not work with NullValueHandling since if the array is empty it is not null, it doesn't have any values at all.
I jus get an error about index is not found.
I am returning the array as a List in my classes.
Assuming you're using Newtonsoft.Json for deserializing the json:
class Data
{
public List<string> Metrics { get; set; }
}
var json = "{\"metrics\":[]}";
var obj = JsonConvert.DeserializeObject<Data>(json);
obj.Metrics will be an empty collection, not null.
Plus, even if it was, you could access it like
var metrics = obj.Metrics ?? new List<string>();

How to receive JSON data into Web API ApiController method?

I'm writing a Web API ApiController with several PUT methods that receive JSON data. The JSON is not deterministic and hence cannot be hard-mapped to a custom C# object, but needs to be received as Dictionaries/Sequences (Maps/Lists).
I have tried using an IDictionary for the data parm of the PUT method in the controller, and this sort of works -- the data appears to be mapped from JSON to the dictionary. However, it's necessary to declare the dictionary as <String,Object>, and there's no clear way to then retrieve the Object values as their appropriate types. (I've found a few suggested kluges in my searching, but they are just that.)
There is also a System.Json.JsonObject type which I finally managed to get loaded via NuGet, but when I use that the system does not appear to know how to map the data.
How is this typically done? How do you implement an ApiController method that receives generic JSON?
I can see three basic approaches:
Somehow make Dictionary/Sequence work with Object or some such.
Make something like System.Json.JsonObject work, perhaps by swizzling the routing info.
Receive the JSON as a byte array and then parse explicitly using one of the C# JSON toolkits available.
(As to how dynamic the data is, JSON objects may have missing entries or extraneous entries, and in some cases a particular entry may be represented as either a single JSON value or a JSON array of values. (Where "value" is JSON array, object, string, number, Boolean, or null.) In general, except for the array/not array ambiguity, the relation between keys and value types is known.)
(But I should note that this is a large project and I'll be receiving JSON strings from several other components by other authors. Being able to examine the received type and assert that it's as expected would be quite useful, and may even be necessary from a security standpoint.)
(I should add that I'm a relative novice with C# -- have only been working with it for about 6 months.)
You've got to know what kind of data you're expecting, but I have had success doing this in the past using dynamic typing.
Something like this:
[Test]
public void JsonTester()
{
string json = "{ 'fruit':'banana', 'color':'yellow' }";
dynamic data = JsonConvert.DeserializeObject(json);
string fruit = data["fruit"];
string color = data["color"];
Assert.That(fruit == "banana");
Assert.That(color == "yellow");
}
Edit:
You either need to know the type you want to deserialize to beforehand - in which case you can deserialize it to that type immediately.
Or you can deserialize it to a dynamic type, and then convert it to your static type once you know what you want to do with it.
using Newtonsoft.Json;
using NUnit.Framework;
public class DTO
{
public string Field1;
public int Field2;
}
public class JsonDeserializationTests
{
[Test]
public void JsonCanBeDeserializedToDTO()
{
string json = "{ 'Field1':'some text', 'Field2':45 }";
var data = JsonConvert.DeserializeObject<DTO>(json);
Assert.That(data.Field1 == "some text");
Assert.That(data.Field2 == 45);
}
[Test]
public void JsonCanBeDeserializedToDynamic_AndConvertedToDTO()
{
string json = "{ 'Field1':'some text', 'Field2':45 }";
var dynamicData = JsonConvert.DeserializeObject<dynamic>(json);
var data = new DTO { Field1 = dynamicData["Field1"], Field2 = dynamicData["Field2"] };
Assert.That(data.Field1 == "some text");
Assert.That(data.Field2 == 45);
}
}

Turn a JSON string into a dynamic object

I'm trying to create a dynamic object from a JSON string in C#. But i can't get it done.
Normally i would get a JSON string through a web service call but in this case I simply created a simple class which I turn into a JSON string. Then I try to turn it back into a dynamic object with the exact same structure as if it was an instance of the Entity class. But that's where I'm having trouble.
This is the class that i turn into a JSON string:
public class Entity
{
public String Name = "Wut";
public String[] Scores = {"aaa", "bbb", "ccc"};
}
Then in somewhere in my code i do this:
var ent = new Entity();
// The Serialize returns this:
// "{\"Name\":\"Wut\",\"Scores\":[\"aaa\",\"bbb\",\"ccc\"]}"
var json = new JavaScriptSerializer().Serialize(ent);
dynamic dynamicObject1 = new JavaScriptSerializer().DeserializeObject(json);
dynamic dynamicObject2 = Json.Decode(json);
When I debug this code then i see that the first dynamicObject1 returns a Dictionary. Not really what I'm after.
The second dynamicObject2 looks more like the Entity object. It has a property called Name with a value. It also has a dynamic array called Scores, but for some reason that list turns out to be empty...
Screenshot of empty Scores property in dynamic object:
So I'm not having any luck so far trying to cast a JSON string to a dynamic object. Anyone any idea how I could get this done?
Using Json.Net
dynamic dynamicObject1 = JsonConvert.DeserializeObject(json);
Console.WriteLine(dynamicObject1.Name);
Using Json.Net but deserializing into a ExpandableOBject, a type that is native to C#:
dynamic obj= JsonConvert.DeserializeObject<ExpandoObject>(yourjson);
If the target type is not specified then it will be convert to JObject type instead. Json object has json types attached to every node that can cause problems when converting the object into other dynamic type like mongo bson.

JavaScriptSerializer: Serialize a list + a string into JSON

I want to use JavaScriptSerializer to send a package of JSON data that contains both a list of objects as well as a string, identified like ChatLogPath. As far as I can tell, that class can only serialize one object -- being the list -- and if I try to append multiple ones it obviously just creates invalid JSON like {...}{...} which won't work.
Is there any way to do this? I'm insanely new to C# and ASP.NET MVC so forgive me if this is a dumb question :)
Edit: here's my code as of right now.
string chatLogPath = "path_to_a_text_file.txt";
IEnumerable<ChatMessage> q = ...
...
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(q) + json.Serialize(chatLogPath);
Which will output the array like this in JSON { ... } followed by the chatLogPath { ... }. In other words, it can't work since that's invalid JSON.
The easiest way to get a single JSON object with the array and path together is to create a class or dynamic object with each as a property/field of it.
Class example:
public class ChatInformation {
public IEnumerable<ChatMessage> messages;
public string chatLogPath;
}
...
var output = new ChatInformation {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);
Dynamic example (requires .NET 4+):
dynamic output = new ExpandoObject {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);
Anonymous Type example (if you don't care to have another class, nor are you on .NET 4):
var output = new {
messages = ...,
chatLogPath = "path_to_a_text_file.txt"
};
return json.Serialize(output);

Categories