使用JSON在ASP.NET和C#服务器端工作 [英] Working with JSON on the Server-Side in ASP.NET and C#

查看:139
本文介绍了使用JSON在ASP.NET和C#服务器端工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是在客户端使用JQuery一个ASP.NET Web表单。我有一个在建的JSON对象的集合,并将其存储的用户界面。随着我的客户端实现完整的,我需要现在的信息化工作,当用户点击一个按钮。点击该按钮,我需要循环通过JSON收集和验证中的条目。我的问题是,我不知道该怎么做。

I have an ASP.NET web form that is using JQuery on the client-side. I have a user interface that is building a collection of objects and storing them in JSON. With my client-side implementation complete, I need to now work with that information when the user clicks a button. When this button is clicked, I need to loop through that JSON collection and validate the entries. My problem is, I'm not sure how to do it.

// Client-Side Code
var myCollection = {
  "data": [
  ]
};


// Server-Side Code
protected void myButton_Click(object sender, EventArgs e)
{
  // Parse JSON
}

集合中的每个项目被存储在数据属性。我如何遍历集合JSON在服务器端?我想到了把JSON数据中隐藏的HTML元素,但是这个不好听,我能想到的一个好办法做到这一点。

Each item in the collection is stored in the "data" property. How do I loop through the JSON collection on the server-side? I thought about putting the JSON data in a Hidden HTML Element, but this didn't sound good and I could think of a good way to do it.

感谢您

推荐答案

如何将它发送到服务器是由你 - 一个隐藏字段,AJAX调用,不管你preFER。一旦你在服务器上的字符串,你需要两件事情:

How you send it to the server is up to you - a hidden field, an AJAX call, whatever you prefer. Once you've got the string on the server, you'll need 2 things:


  1. A C#服务器端重新presentation
    该对象的

  2. A转换器去
    从JSON到C#重presentation。

让我们调整的例子有点,因为MyCollection的在你的例子是一个对象,而不是一个集合。因此,我们将myObject的调用它。其次,我们假设的数据是一个字符串数组。它可以是任何东西,但我们会保持它的简单。

Let's adjust your example a bit, because "myCollection" in your example is an object, not a collection. So we'll call it myObject. Secondly, we'll assume that "data" is an array of strings. It could be anything, but we'll keep it simple.

var myObject = {
  data: ["string1","string2"]
};

我们还假设您正在使用DataContractJsonSerializer,这样你就可以很容易地映射两种不同的情况下,风格... JavaScript是典型的驼峰,和C#通常是ProperCase。因此,在C#中,这将是:

We'll also assume you're using the DataContractJsonSerializer, so you can easily map the two different case-styles...JavaScript is typically camelCase, and C# is typically ProperCase. So, in C#, this would be:

[DataContract(Name="myObjectType")]
public class MyObjectType{
  [DataMember(Name="data")]
  public string[] Data { get; set; }
}

现在你有相同结构的两个重presentations,一个在C#中,一个在JavaScript的。从一个转换到另一个,我们可以使用内置的DataContractJsonSerializer,像这样的:

Now you have two representations of the same structure, one in c#, one in JavaScript. To convert from one to the other, we can use the built-in DataContractJsonSerializer, like this:

public static T Deserialize<T>(string json)
{
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
    {
      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
      return (T)serializer.ReadObject(ms);
    }
}

...导致的最终调用:

...resulting in a final call of:

MyObjectType myObject = Deserialize<MyObjectType>(incomingString);

这篇关于使用JSON在ASP.NET和C#服务器端工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆