简体   繁体   English

将JSON字符串反序列化为c#对象

[英]Deserialize JSON string to c# object

My Application is in Asp.Net MVC3 coded in C#. 我的应用程序是在用C#编码的Asp.Net MVC3中。 This is what my requirement is. 这就是我的要求。 I want an object which is in the following format.This object should be achieved when I deserialize the Json string. 我想要一个以下格式的对象。当我反序列化Json字符串时,应该实现这个对象。

var obj1 = new { arg1=1,arg2=2 };

在此输入图像描述

After using the below code: 使用以下代码后:

string str = "{\"Arg1\":\"Arg1Value\",\"Arg2\":\"Arg2Value\"}";
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
object obje = serializer1.Deserialize<object>(str);

The object what i get ie obje does not acts as obj1 我得到的对象即obje不作为obj1

在此输入图像描述

Here, in this example my JSON string is static, but actually JSON string is going to be dynamically generated runtime, so i won't be able get Arg1 and Arg2 all the time. 在这个示例中,我的JSON字符串是静态的,但实际上JSON字符串将是动态生成的运行时,因此我无法一直获得Arg1和Arg2。

I think the JavaScriptSerializer does not create a dynamic object. 我认为JavaScriptSerializer不会创建动态对象。

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 : 并反序列化而不是object

serializer.Deserialize<MyObj>(str);

Not testet, please try. 不是测试,请尝试。

I believe you are looking for this: 我相信你在寻找这个:

string str = "{\"Arg1\":\"Arg1Value\",\"Arg2\":\"Arg2Value\"}";
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
object obje = serializer1.Deserialize(str, obj1.GetType());

使用此代码:

var result=JsonConvert.DeserializeObject<List<yourObj>>(jsonString);

This may be useful: 这可能很有用:

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

Where "json" is the string that contains the JSON values. 其中“json”是包含JSON值的字符串。 Then to retrieve the values from the jsonObject you may use 然后从您可能使用的jsonObject中检索值

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

Changing MyPropertyType to the proper type (ToInt32, ToString, ToBoolean, etc). 将MyPropertyType更改为正确的类型(ToInt32,ToString,ToBoolean等)。

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: 因此,如果服务将响应作为JSON字符串返回,则必须首先对字符串进行反序列化,然后您可以正确地反序列化对象类型:

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.... ;) 4当然可以工作......;)

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; }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM