将 Json 文本解析为 asp mvc 4 中的 C# 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13489061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:44:09  来源:igfitidea点击:

parse Json text to C# object in asp mvc 4

c#.netasp.net-mvcjsonasp.net-mvc-4

提问by Hilmi

I have a huge amount of customized attributes I want to save them in the DataBase, I was confused of how to store them in the database, i thought of storing them as a string separating them by

我有大量的自定义属性我想将它们保存在数据库中,我对如何将它们存储在数据库中感到困惑,我想将它们存储为一个字符串

(==> name , value) (;=> attribute , attribute) but the code wasn't elegant at all!

( ==> name , value) ( ;=> attribute , attribute) 但代码一点都不优雅!

so i stat thinking of saving them as Jsonstring but I couldn't found a Json to object parser

所以我想将它们保存为Json字符串,但我找不到 Json to object parser

while we need only to call json()to parse object to json string

而我们只需要调用json()解析object to json string

is there a better way than using json string and is there json string parser provided ?

有没有比使用 json 字符串更好的方法,是否提供了 json 字符串解析器?

采纳答案by dove

Many people use Json.netfor serialization

很多人使用Json.net进行序列化

var log  = JsonConvert.DeserializeObject<YourObject>(logJson)

and the other direction

另一个方向

  var logJson = JsonConvert.SerializeObject(log);

回答by GiantHornet

You can use $.parseJSON, try this just for you to see the txt data:

你可以使用 $.parseJSON,试试这个只是为了你看 txt 数据:

var info = $.parseJSON(data);
 alert(info);

回答by testCoder

Try to use System.Web.Script.Serialization.JavaScriptSerializer, here is example:

尝试使用 System.Web.Script.Serialization.JavaScriptSerializer,这是示例:

var yourObject = new JavaScriptSerializer().Deserialize<YourType>(strInput)

or

或者

var yourObject = new JavaScriptSerializer().Deserialize(strInput)