Deserialize JSON string to c# object

后端 未结 6 2081
轻奢々
轻奢々 2020-12-10 01:15

My Application is in Asp.Net MVC3 coded in C#. This is what my requirement is. I want an object which is in the following format.This object should be achieved when I deseri

相关标签:
6条回答
  • 2020-12-10 01:51

    I think the JavaScriptSerializer does not create a dynamic object.

    So you should define the class first:

    class MyObj {
        public int arg1 {get;set;}
        public int arg2 {get;set;}
    }
    

    And deserialize that instead of object:

    serializer.Deserialize<MyObj>(str);
    

    Not testet, please try.

    0 讨论(0)
  • 2020-12-10 01:51

    Use this code:

    var result=JsonConvert.DeserializeObject<List<yourObj>>(jsonString);
    
    0 讨论(0)
  • 2020-12-10 02:03

    solution :

     public Response Get(string jsonData) {
         var json = JsonConvert.DeserializeObject<modelname>(jsonData);
         var data = StoredProcedure.procedureName(json.Parameter, json.Parameter, json.Parameter, json.Parameter);
         return data;
     }
    

    model:

     public class modelname {
         public long parameter{ get; set; }
         public int parameter{ get; set; }
         public int parameter{ get; set; }
         public string parameter{ get; set; }
     }
    
    0 讨论(0)
  • 2020-12-10 02:07

    I believe you are looking for this:

    string str = "{\"Arg1\":\"Arg1Value\",\"Arg2\":\"Arg2Value\"}";
    JavaScriptSerializer serializer1 = new JavaScriptSerializer();
    object obje = serializer1.Deserialize(str, obj1.GetType());
    
    0 讨论(0)
  • 2020-12-10 02:07

    This may be useful:

    var serializer = new JavaScriptSerializer();
    dynamic jsonObject = serializer.Deserialize<dynamic>(json);
    

    Where "json" is the string that contains the JSON values. Then to retrieve the values from the jsonObject you may use

    myProperty = Convert.MyPropertyType(jsonObject["myProperty"]);
    

    Changing MyPropertyType to the proper type (ToInt32, ToString, ToBoolean, etc).

    0 讨论(0)
  • 2020-12-10 02:13

    Same problem happened to me. So if the service returns the response as a JSON string you have to deserialize the string first, then you will be able to deserialize the object type from it properly:

    string json= string.Empty;
    using (var streamReader = new StreamReader(response.GetResponseStream(), true))
            {
                json= new JavaScriptSerializer().Deserialize<string>(streamReader.ReadToEnd());
    
            }
    //To deserialize to your object type...
    MyType myType;
    using (var memoryStream = new MemoryStream())
             {
                byte[] jsonBytes = Encoding.UTF8.GetBytes(@json);
                memoryStream.Write(jsonBytes, 0, jsonBytes.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);
                using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(memoryStream, Encoding.UTF8,          XmlDictionaryReaderQuotas.Max, null))
                {
                    var serializer = new DataContractJsonSerializer(typeof(MyType));
                    myType = (MyType)serializer.ReadObject(jsonReader);
    
                }
            }
    

    4 Sure it will work.... ;)

    0 讨论(0)
提交回复
热议问题