1

I have a below code that used JSON.stringify to the object then passed it on POST method (Please see below Javascript code). I'm getting those values on the backend using C#. My problem is, how could I convert/manipulate/access the stringified values. Please see below C# code

Javascript:

var json_db = JSON.stringify(selectedDbInfo);
$.post("../FormActions/DatabaseChanges.aspx", { action: "savedb", orderNumber: orderNumber, selectedDb: json_db}, 
    function (response) {
        alert('ok');
    });

C#:

var dbValue = c.Request.Params["selectedDb"];

below is the result value of dbValue

"[{\"dbname\":\"BASINS\",\"distance\":\"0\"},{\"dbname\":\"BROWNFIELD\",\"distance\":\"0.5\"},{\"dbname\":\"BRS\",\"distance\":\"0\"}]"
Starscream1984
  • 3,072
  • 1
  • 19
  • 27
Mark
  • 314
  • 1
  • 5
  • 17
  • See this [link][1] for your answer. You'll have to use JArray.Parse though. [1]: http://stackoverflow.com/questions/22955990/json-jobject-detect-multiple-array-objects/22958179#22958179 – Dbl Apr 09 '14 at 09:54

4 Answers4

0

You need to parse the JSON into a .NET array or List.

Many use json.NET for this: http://james.newtonking.com/json

At a push you could use some string manipulation to populate your objects one by one, but I wouldn't recommend that.

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
0

There are many samples here on SO. if you're just want to convert it dictionary look here: How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

However, there's a built in mechanism in ASP.NET MVC that serializes automatically your json param to predefined objects at your convenient.

Community
  • 1
  • 1
Niro
  • 394
  • 1
  • 2
  • 12
0

You may define a class having the fields like dbname and distance as properties. Then you may deserialize the json string dbValue into a list of that type using NewtonSoft.Json. Please see the code below:

var list = JsonConvert.DeserializeObject<List<RootObject>>(dbValue);

foreach (var item in list)
{
    Console.WriteLine(string.Format("dbname: {0}, distance: {1}", item.dbname, item.distance));
}

Ans the definition of RootObject is as simple as you guess:

public class RootObject
{
    public string dbname { get; set; }
    public string distance { get; set; }
}
Wasif Hossain
  • 3,900
  • 1
  • 18
  • 20
0

Create a custom serializable data contract class, say DatabaseDistance, with following properties:

[DataMember(Name = "dbname")]
private string name;

[DataMember(Name = "distance")]
private double distance;

and use following method for deserialization:

public static T FromJSON<T>(string jsonValue, IEnumerable<Type> knownTypes)
            {
                //validate input parameters here

                T result = default(T);

                try
                {
                    using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(jsonValue)))
                    {                   
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T), knownTypes);
                        result = (T)serializer.ReadObject(stream);
                    }
                }
                catch (Exception exception)
                {
                    throw new Exception("An error occurred while deserializing", exception);
                }

                return result;
            }

pass list of your objects as type parameter

Andrew
  • 3,648
  • 1
  • 15
  • 29