views:

386

answers:

3

Hi!

I've successfully created code that serializes a complex javascript object and posts it to an ASP.NET page. I can get the JSON string using Request.Form and the result looks like this (I've added indentation for readability):

    {
    "gasterPerStolPerVecka": {
        "name": "gasterPerStolPerVecka",
        "keyValue_id": "2",
        "set_id": "1",
        "containerElement": "#gasterPerStolPerVecka",
        "keyValueComponents": [
            {
                "name": "gasterPerStolPerVecka_guestsPerWeek",
                "value": "200"
            },
            {
                "name": "gasterPerStolPerVecka_numberOfChairs",
                "value": "100"
            }
        ],
        "keyValueUnitText": "gäster/stol per vecka",
        "keyValueCalculationMethod": "divide",
        "isValid": true,
        "result": 2
    },
    "tillgangligaStolstimmarPerVecka": {
        "name": "tillgangligaStolstimmarPerVecka",
        "keyValue_id": "1",
        "set_id": "1",
        "containerElement": "#tillgangligaStolstimmarPerVecka",
        "keyValueComponents": [
            {
                "name": "tillgangligaStolstimmarPerVecka_openHoursPerWeek",
                "value": "35"
            },
            {
                "name": "tillgangligaStolstimmarPerVecka_numberOfChairs",
                "value": "100"
            }
        ],
        "keyValueUnitText": "stolstimmar/vecka",
        "keyValueCalculationMethod": "multiply",
        "isValid": true,
        "result": 3500
    },
    "planeradIntaktPerTillgangligStolOchVecka": {
        "name": "planeradIntaktPerTillgangligStolOchVecka",
        "keyValue_id": "",
        "set_id": "",
        "containerElement": "#planeradIntaktPerTillgangligStolOchVecka",
        "keyValueComponents": [
            {
                "name": "planeradIntaktPerTillgangligStolOchVecka_weeklyRevenue",
                "value": ""
            },
            {
                "name": "planeradIntaktPerTillgangligStolOchVecka_numberOfChairs",
                "value": "100"
            }
        ],
        "keyValueUnitText": "kr",
        "keyValueCalculationMethod": "divide",
        "isValid": false,
        "result": null,
        "errorText": "Ofullständigt underlag för beräkning."
    },
    "planeradIntaktPerTillgangligaStolstimmar": {
        "name": "planeradIntaktPerTillgangligaStolstimmar",
        "keyValue_id": "",
        "set_id": "",
        "containerElement": "#planeradIntaktPerTillgangligaStolstimmar",
        "keyValueComponents": [
            {
                "name": "planeradIntaktPerTillgangligaStolstimmar_weeklyRevenue",
                "value": ""
            },
            {
                "name": "planeradIntaktPerTillgangligaStolstimmar_openHoursPerWeek",
                "value": "35"
            },
            {
                "name": "planeradIntaktPerTillgangligaStolstimmar_numberOfChairs",
                "value": "100"
            }
        ],
        "keyValueUnitText": "kr",
        "keyValueCalculationMethod": "divide_divide",
        "isValid": false,
        "result": null,
        "errorText": "Ofullständigt underlag för beräkning."
    }
}

Now I try to deserialize this on the server side, but it's difficult. I keep getting the error:

[NullReferenceException: Object reference not set to an instance of an object.]

I don't know where to start looking for errors?

Thanks in advance! /Thomas Kahn

A: 

You need to use a deserialization library for ASP.NET. See http://json.org/ for libraries that are available or maybe there is one built into ASP.NET. Either way, the code will look like:

String s = getAppropriateFormField();
Object o = JSONLibraryPackage.parse(s);

where obviously you'll have to fill in the blanks for how you get the form field and then what package and method does the parsing.

Bialecki
A: 

use stringify instead serialization

A: 

Thanks for replying! Here's some more background information regarding what I'm trying to do.

The actual deserialization syntax (ASP.NET 2.0) that I'm using looks like this:

Nyckeltal nyckeltal = new JavaScriptSerializer().Deserialize<Nyckeltal>(tempString);

The class "Nyckeltal" (Key Value in Swedish) looks like this. There is also a class called "Nyckeltalskomponent" (Key Value Component in Swedish) that is used to store a list of key value components in the "Nyckeltal" object:

public class Nyckeltal

{ public Nyckeltal() { }

public string name {set; get;} public string keyValue_id { set; get; } public string set_id { set; get; } public string containerElement { set; get; } public List keyValueComponents { set; get; } public string keyValueUnitText { set; get; } public string keyValueCalculationMethod { set; get; } public bool isValid { set; get; } public string result { set; get; } }

public class Nyckeltalskomponent { public Nyckeltalskomponent() { }

public string name {set; get;} public string value {set; get;} }

And I have it working with a JSON test string that looks like this:

{
    "name": "gasterPerStolPerVecka",
    "keyValue_id": "2",
    "set_id": "1",
    "containerElement": "#gasterPerStolPerVecka",
    "keyValueComponents": [
        {
            "name": "gasterPerStolPerVecka_guestsPerWeek",
            "value": "200"
        },
        {
            "name": "gasterPerStolPerVecka_numberOfChairs",
            "value": "100"
        }
    ],
    "keyValueUnitText": "gäster/stol per vecka",
    "keyValueCalculationMethod": "divide",
    "isValid": true,
    "result": 2,
    "errorText": "Ofullständigt underlag för beräkning."
}

The problem is that this is a single JSON object. In the JSON string that I'm getting back from the page, there are several JSON objects (members) encapsulated in one object. Each memnber has a name/key. The syntax is this (simplified):

{"obj1":{},"obj2":{},"obj3":{}}

According to JSONLint, this syntax is fully valid so I'm suspecting that the problems I'm experiencing are caused by:

  1. My server side classes
  2. The way I'm using JavaScriptSerializer
  3. Some limitation in the JavaScriptSerializer class

When I attempt to deserialize several JSON objects, I use this class:

public class AllaNyckeltal { public AllaNyckeltal() { } public List nyckeltal { set; get; } }

And the ASP.NET code looks like this:

string tempString = Request.Form[0];
AllaNyckeltal allaNyckeltal = new JavaScriptSerializer().Deserialize<AllaNyckeltal>(tempString);
Response.Write(allaNyckeltal.nyckeltal.Count.ToString());

The error I get when I try this is Object reference not set to an instance of an object.

It's hopeful that I'm getting some results, I just think I need some expert help to cross the finish line.

/Thomas Kahn

tkahn