简体   繁体   English

如何制作MVC将C#对象转换为json并将其发送到客户端,并接收json并将其转换为C#对象?

[英]How do I make a MVC to convert C# object to json and send it to client, and receive json and convert it to a C# object?

I'm trying to a make an MVC (but without the view), that sends and receives json objects, but the objects it sends are first in C#, and the objects it recieves in json have to be converted to C# objects. 我正在尝试制作一个MVC(但没有视图),该MVC发送和接收json对象,但是它发送的对象首先在C#中进行,并且它在json中接收的对象必须转换为C#对象。 I have very little knowledge of json, and not much knowledge of using C# in this manner, but I have taken a crack at it by first creating two classes in the model folder (or partially creating), one called SendData, the other called ReceivedData, and defining the variable types of the object that will be converted into json or from json. 我对json的知识很少,而对以这种方式使用C#的知识也不多,但是我首先在模型文件夹中创建了两个类(或部分创建了),一个名为SendData,另一个称为ReceivedData ,并定义将转换为json或从json转换的对象的变量类型。

Description of system: Have an MVC that gets data from an SQL database, converting the C# object into a json, and sends the json to a client app. 系统描述:具有一个MVC,该MVC从SQL数据库获取数据,将C#对象转换为json,然后将json发送到客户端应用程序。 Client app sends a different json to MVC that needs to be converted into a C# object which is used to update database (or is a request for an update from database). 客户端应用向MVC发送一个不同的json,该json需要转换为C#对象,该对象用于更新数据库(或来自数据库的更新请求)。

The MVC will recieve json that contains several variables and a list of items (of the same type). MVC将接收包含多个变量和项目列表(相同类型)的json。 It will send a json that contains several variables, and a list of objects/data contained in multiple objects (eg: ObjectListName:[ {variable1fromobject1:data, varibale2fromobject1:data, variable3fromobject1:data} {variable1fromobject2:data, variable2fromobject2:data, variable3fromobject2:data}] ) 它将发送一个包含多个变量的json,以及包含在多个对象中的对象/数据的列表(例如:ObjectListName:[{variable1fromobject1:data,varibale2fromobject1:data,variable3fromobject1:data} {variable1fromobject2:data,variable2fromobject2:data,variable3fromobject2 :data}])

How do I set this up for the conversion of received data to C#, and conversion of data to be sent into json? 如何设置此功能以将接收到的数据转换为C#,以及将要发送的数据转换为json?

Newtonsoft Json.NET would help you a lot :) Newtonsoft Json.NET将为您提供很多帮助:)

http://james.newtonking.com/json http://james.newtonking.com/json

JsonConvert.DeserializeObject(jsonstring); JsonConvert.DeserializeObject(jsonstring);

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

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