解析JSON响应的最简单方法

时间:2021-08-04 14:05:05

Is there any easy way to parse below JSOn in c#

有什么简单的方法可以在c#中解析JSOn下面的内容吗

{"type":"text","totalprice":"0.0045","totalgsm":"1","remaincredit":"44.92293","messages": [
{"status":"1","messageid":"234011120530636881","gsm":"923122699633"}
]}

and in case Multiple results.

如果有多个结果。

4 个解决方案

#1


40  

Follow these steps:

遵循以下步骤:

  1. Convert your JSON to C# using json2csharp.com;
  2. 使用json2csharp.com将JSON转换为c#;
  3. Create a class file and put the above generated code in there;
  4. 创建一个类文件并将上面生成的代码放在其中;
  5. Add the Newtonsoft.Json library to your project using the Nuget Package Manager;
  6. 添加Newtonsoft。使用Nuget包管理器向您的项目提供Json库;
  7. Convert the JSON received from your service using this code:

    使用以下代码转换从服务接收的JSON:

     RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
    

(Feel free to rename RootObject to something more meaningful to you. The other classes should remain unchanged.)

(你可以把RootObject改名为对你更有意义的东西。其他类应该保持不变。

#2


19  

You can safely use built-in JavaScriptSerializer without referencing additional third party libraries:

您可以安全地使用内置的JavaScriptSerializer,而无需引用其他第三方库:

var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
ser.DeserializeObject(json);

#3


4  

I found a way to get it without using any external API

我找到了一种不用任何外部API就能得到它的方法

        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            string url = "YOUR URL";
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                var result = jsSerializer.DeserializeObject(json_data);
                Dictionary<string, object> obj2 = new Dictionary<string, object>();
                obj2=(Dictionary<string,object>)(result);

                string val=obj2["KEYNAME"].ToString();
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance 
        }

#4


1  

For me ... the easiest way to do that is using JSON.net do a deserialize to a entity that represents the object, for example:

对我来说……最简单的方法是使用JSON.net对表示对象的实体进行反序列化,例如:

public class Message
{
    public string status { get; set; }
    public string messageid { get; set; }
    public string gsm { get; set; }
}

public class YourRootEntity
{
    public string type { get; set; }
    public string totalprice { get; set; }
    public string totalgsm { get; set; }
    public string remaincredit { get; set; }
    public List<Message> messages { get; set; }
}

And do this:

这样做:

YourRootEntity data JsonConvert.DeserializeObject<YourRootEntity>(jsonStrong);

#1


40  

Follow these steps:

遵循以下步骤:

  1. Convert your JSON to C# using json2csharp.com;
  2. 使用json2csharp.com将JSON转换为c#;
  3. Create a class file and put the above generated code in there;
  4. 创建一个类文件并将上面生成的代码放在其中;
  5. Add the Newtonsoft.Json library to your project using the Nuget Package Manager;
  6. 添加Newtonsoft。使用Nuget包管理器向您的项目提供Json库;
  7. Convert the JSON received from your service using this code:

    使用以下代码转换从服务接收的JSON:

     RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
    

(Feel free to rename RootObject to something more meaningful to you. The other classes should remain unchanged.)

(你可以把RootObject改名为对你更有意义的东西。其他类应该保持不变。

#2


19  

You can safely use built-in JavaScriptSerializer without referencing additional third party libraries:

您可以安全地使用内置的JavaScriptSerializer,而无需引用其他第三方库:

var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
ser.DeserializeObject(json);

#3


4  

I found a way to get it without using any external API

我找到了一种不用任何外部API就能得到它的方法

        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            string url = "YOUR URL";
            // attempt to download JSON data as a string
            try
            {
                json_data = w.DownloadString(url);
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                var result = jsSerializer.DeserializeObject(json_data);
                Dictionary<string, object> obj2 = new Dictionary<string, object>();
                obj2=(Dictionary<string,object>)(result);

                string val=obj2["KEYNAME"].ToString();
            }
            catch (Exception) { }
            // if string with JSON data is not empty, deserialize it to class and return its instance 
        }

#4


1  

For me ... the easiest way to do that is using JSON.net do a deserialize to a entity that represents the object, for example:

对我来说……最简单的方法是使用JSON.net对表示对象的实体进行反序列化,例如:

public class Message
{
    public string status { get; set; }
    public string messageid { get; set; }
    public string gsm { get; set; }
}

public class YourRootEntity
{
    public string type { get; set; }
    public string totalprice { get; set; }
    public string totalgsm { get; set; }
    public string remaincredit { get; set; }
    public List<Message> messages { get; set; }
}

And do this:

这样做:

YourRootEntity data JsonConvert.DeserializeObject<YourRootEntity>(jsonStrong);