Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net中解析Json对象_C#_Asp.net_Json - Fatal编程技术网

C# 在asp.net中解析Json对象

C# 在asp.net中解析Json对象,c#,asp.net,json,C#,Asp.net,Json,我在Asp.net web表单的页面加载方法中使用了以下代码列表: var jsonSerializer = new JavaScriptSerializer(); var jsonString = String.Empty; context.Request.InputStream.Position = 0; using (var inputStream = new StreamReader(context.Request

我在Asp.net web表单的页面加载方法中使用了以下代码列表:

var jsonSerializer = new JavaScriptSerializer();
            var jsonString = String.Empty;

            context.Request.InputStream.Position = 0;
            using (var inputStream = new StreamReader(context.Request.InputStream))
            {
                jsonString = inputStream.ReadToEnd();
            }

            var emplList = jsonSerializer.Deserialize<List<Employee>>(jsonString);
            var resp = String.Empty;

            foreach (var emp in emplList)
            {
                resp += emp.name + " \\ ";
                //File.AppendAllText(@"d:\status\LOL.txt", emp.name.ToString()+"\r\n", Encoding.UTF8);
            }

            context.Response.ContentType = "application/json";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.Write(jsonSerializer.Serialize(resp));


            File.AppendAllText(@"d:\status\LOL.txt", resp.ToString() + "\r\n", Encoding.UTF8);

        }
        catch (Exception)
        {
            File.AppendAllText(@"d:\status\LOL.txt", "Stop it!", Encoding.UTF8);

        }

当我将JSON发送到这个aspx文件时,object接收它并将其保存到文件中,读取它,但是我如何使用JSON object解析变量'resp'?

我建议您忘记JavaScriptSerializer,使用newtownsoft的JSON实现。它更快、更容易使用。有关使用它的文档如下:

您可以使用Nuget下载/安装它

下面是他们文档网站上的一个快速代码片段示例:

Product product = new Product();

product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = JsonConvert.SerializeObject(product);
//{
//  "Name": "Apple",
//  "ExpiryDate": "2008-12-28T00:00:00",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}

Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

不清楚你想做什么。resp只是emplist中的一个名称字符串,如:Bob\Tom\Joe,它不是JSON对象。你想从中解析什么?好的,用Pike编码你告诉我如何在我的asp.net web表单中获取JSON对象?谢谢我的朋友,但是我如何在我的web表单页面加载方法中获取JSON对象?这个解决方案获取JSON对象?我的web表单如何理解JSON对象接收?@BehzadRazzaqi哦,这取决于你在尝试做什么。您可以通过多种方式在页面加载方法中获取JSON对象。您可以使用上面的代码片段作为指导,从头开始创建它。或者你可以从文件中读取它。或者你可以查询一个数据库并转换它,等等。我肯定这不是你要问的,所以我需要一些关于你需要做什么来提供更多指导的更多细节。