5

I'm using the JSONPEncoderFactory,JSONPBehavior solution to enable JSONP in WCF. That's all fine, it's all set up and working well, my service returns the data correctly, no problems there.

However, I need to be able to force the JSON variable names into lowercase due to the way they are being used in JS, and this is something I haven't been able to figure out as yet.

Here is an example of my service output (the variable names and values have been changed to benign elements for this example)

{"Animals":["dog","cat","mouse"],"Owner":"Greg","Permanent":"y","ID":1,"DaysToStay":"1"}

Pretty simple right? I want the "Animals" to be "animals", and so on...

Do I need to use a json parser for this, or is it easy enough just to use a regular expression? I'd be grateful if someone could let me know how they've done this before.

Thanks!

2
  • 2
    Are you using DataContract based serialization? If so, you can decorate your property with [DataMember("animals")] etc. Commented Sep 4, 2011 at 18:42
  • If you are generating this in C# you can use .ToLower() on the string. Commented Sep 4, 2011 at 18:47

1 Answer 1

2

You can use this function on JavaScript:

FN = function (obj)
{
    var ret = null;
    if (typeof(obj) == "string" || typeof(obj) == "number")
        return obj;
    else if (obj.push)
        ret = [];
    else
        ret = {};

    for (var key in obj)
        ret[String(key).toLowerCase()] = FN(obj[key]);
    return ret;
};

EDIT: Deserialize a json string in a Dictionary with C#:

using System.Web.Script.Serialization;
var serializer = new JavaScriptSerializer();
var dic = serializer.Deserialize<Dictionary<string,dynamic>>(yourJSONString);

The complex fields will be deserialized into Dictionary. So you will ned a recursive function for inspect the matherialized dic.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response, but this looks like a JavaScript function. I'm really looking for a way to do this in ASP.NET just before the stream gets returned from the service as JSON.
In C# you can desserialize your json string in a Dictionary. Iterate over it and then, build a new dictionary with your keys in lower case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.