我刚刚开始使用淘汰赛,而且我在使用JavaScriptSerializer进行DateTime序列化和反序列化时遇到了麻烦.
我在他的博客中更新了Steves koListEditor示例中的礼物模型,以包含Modified DateTime字段:
public class GiftModel
{
public string Title { get; set; }
public double Price { get; set; }
public DateTime Modified { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我更新了Index.aspx以包含新字段:
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<h1>Gift list editor</h1>
<p>You have asked for <span data-bind="text: gifts().length"> </span> gift(s)</p>
<form class="giftListEditor">
<table>
<tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody>
</table>
<button data-bind="click: addGift">Add Gift</button>
<button data-bind="enable: gifts().length > 0" type="submit">Submit</button>
</form>
<script type="text/html" id="giftRowTemplate">
<tr>
<td>Gift name: <input class="required" data-bind="value: …Run Code Online (Sandbox Code Playgroud) 我将我的实体框架实体从我的Web项目和数据访问层拆分为一个单独的类库.在我的控制器中,我调用我的存储库来获取IEnumerable<RobotDog.Entities.Movie>,然后尝试序列化为json使用JavaScriptSerializer但是我得到一个循环引用,即使我正在使用该[ScriptIgnore]属性.
重要提示:最初我在一个项目下拥有我的实体,数据访问和网络,我能够在没有循环引用的情况下成功序列化我的实体.当我创建单独的图层时,我开始遇到问题.我没有改变任何实体.
RobotDog.Entities命名空间中我的一个实体的示例:
namespace RobotDog.Entities {
public class Character {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(200)]
public string Name { get; set; }
public virtual Person Person { get; set; }
[ScriptIgnore]
public virtual Movie Movie { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
namespace RobotDog.Web.Controllers {
public class MoviesController : Controller {
private UnitOfWork _unitOfWork = new UnitOfWork();
[HttpGet]
public ActionResult Index() {
var user = Membership.GetUser(User.Identity.Name);
if(user …Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework circular-reference javascriptserializer
我已经为vscode安装了c#支持(版本是1.15.0)并通过创建了一个HelloWorld项目dotnet new console.
现在Program.cs,我想使用JavaScriptSerializer依赖于System.Web.Extensions汇编的类.我键入using System.Web.Script.Serialization;并运行dotnet restore,但vscode无法解析它.错误是,
命名空间"System"中不存在类型或命名空间名称"Web"(您是否缺少程序集引用?)
它似乎System.Web不是.net核心的一部分,但有没有办法将程序集添加到项目中?
我找不到project.json其他帖子中提到的文件,因为它是一个csproj项目.
[Serializable]
public class ModelResource:ISerializable
{
public Int64 Ore { get; private set; }
public Int64 Crystal { get; private set; }
public Int64 Hydrogen { get; private set; }
//needs to be ignored
public Int64 Total { get { return Ore + Hydrogen + Crystal; } }
public string ResourceType { get; private set; }
public Int64 HerculesNeeded { get { return Total / 25000; } }
public Int64 AtlasNeeded { get { return Total / 5000; } }
public …Run Code Online (Sandbox Code Playgroud) 我正在创建一个asp.net 2.0 webservice,它将json作为输出,并且有一个非常大的,不能分解,超出最大长度限制的数据集
我在互联网上搜索,有.net 3.5和4的解决方案,但不是2.0.
可以告诉我如何增加JSON限制?
我开发了一个asp.net Web应用程序,最初使用内置的javascript序列化程序将一些数据序列化为json字符串.这成了问题,因为我后来发现有一个关于我能够序列化的数据量的问题.在使用JSON JavaScriptSerializer进行序列化或反序列化期间,我一直收到""错误.字符串的长度超过了maxJsonLength属性上设置的值".通过使用json.net序列化/反序列化我的数据很快就解决了这个问题.这意味着使用json.net序列化/反序列化时没有预设的最大大小或者有一个限制高于javascriptserializer使用的限制.
我的问题很简单.在使用json.net进行序列化/反序列化时,我将来会遇到类似的问题吗?即使用json.net序列化数据时是否有大小限制,或者我可以假设没有限制,我的程序应该没问题,因为数据库的大小增加了?我在json.net的讨论论坛上问了这个问题,但没有收到回复.我希望有人知道答案.提前致谢.
Json字符串:
{"movies":[{"id":"1","title":"Sherlock"},{"id":"2","title":"The Matrix"}]}
Run Code Online (Sandbox Code Playgroud)
C#类:
public class Movie {
public string title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
C#将json转换为电影的c#列表:
JavaScriptSerializer jss = new JavaScriptSerializer();
List<Movie> movies = jss.Deserialize<List<Movie>>(jsonString);
Run Code Online (Sandbox Code Playgroud)
我的movies变量最终变成了一个count = 0的空列表.我错过了什么吗?
我有一个List返回类型的函数.我在支持JSON的WebService中使用它,如:
Run Code Online (Sandbox Code Playgroud)[WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public List<Product> GetProducts(string dummy) /* without a parameter, it will not go through */ { return new x.GetProducts(); }
这会返回:
{"d":[{"__type":"Product","Id":"2316","Name":"Big Something ","Price":"3000","Quantity":"5"}]}
Run Code Online (Sandbox Code Playgroud)
我需要在一个简单的aspx文件中使用这个代码,所以我创建了一个JavaScriptSerializer:
JavaScriptSerializer js = new JavaScriptSerializer();
StringBuilder sb = new StringBuilder();
List<Product> products = base.GetProducts();
js.RegisterConverters(new JavaScriptConverter[] { new ProductConverter() });
js.Serialize(products, sb);
string _jsonShopbasket = sb.ToString();
Run Code Online (Sandbox Code Playgroud)
但它返回没有类型:
[{"Id":"2316","Name":"Big One ","Price":"3000","Quantity":"5"}]
Run Code Online (Sandbox Code Playgroud)
有没有人有任何线索如何让第二个序列化像第一个一样工作?
谢谢!
我在db中有一个存储json字符串的字段,当我在json结果中返回它时,我想要它将作为json原始数据返回,而不是用引号作为字符串扭曲.
UPDATE 1(更多信息):
如果你看图像字段它包含一个原始的json字符串值
但是在用JsonResult序列化之后它会被引号扭曲,因为它是一种String,我怎么能告诉序列化器将图像字段视为原始json数据?
var db = new ModelsContainer();
var res = db.Images.OrderByDescending(i=>i.DateCreated).Skip(skip).Take(take).Select( i => new {
id = i.Id,
dateCreated = i.DateCreated,
images = i.Images ,
user = new {
id = i.User.Id,
facebookId = i.User.FacebookId,
displayName = i.User.DisplayName
},
tags = i.Tags.Select( t => t.Value )
}).ToList();
return Json(res, JsonRequestBehavior.AllowGet);
[
{
"id":"5c528e88-f3a7-4b30-9746-980867325fd1",
"dateCreated":"\/Date(1364381593000)\/",
"images":"[{\"source\":\"http://localhost:9242/images/f4956702/6d34/42db/b28a/397d0eaf3097.jpg\",\"width\":237,\"height\":237},{\"source\":\"http://localhost:9242/images/87d47041/1522/4d10/9325/105851aae259.jpg\",\"width\":633,\"height\":633},{\"source\":\"http://localhost:9242/images/2a639272/9067/42fb/83ee/e88f0a0878f8.jpg\",\"width\":547,\"height\":547},{\"source\":\"http://localhost:9242/images/37caa7b2/e183/4efc/96eb/487e556501b2.jpg\",\"width\":1024,\"height\":1024}]",
"user":{"id":"ea39616d-6ff9-424b-b99b-7bee53e674bb","facebookId":"608215901","displayName":"Yonathan Garti"},
"tags":["test","test","test"]
},
...
]
Run Code Online (Sandbox Code Playgroud) 我成功地使用MVC3中的JavaScriptSerializer将json字符串反序列化为动态对象.我无法弄清楚的是如何将它投射到我可以列举的东西上.下面的foreach代码行是我最新的attemt,但它错误:"无法将类型'System.Dynamic.DynamicObject'隐式转换为'System.Collections.IEnumerable'.如何转换或转换以便我可以遍历字典?
public dynamic GetEntities(string entityName, string entityField)
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new MyProject.Extensions.JsonExtension.DynamicJsonConverter() });
dynamic data = serializer.Deserialize(json, typeof(object));
return data;
}
foreach (var author in GetEntities("author", "lastname"))
Run Code Online (Sandbox Code Playgroud) c# ×5
json ×3
asp.net ×2
asp.net-mvc ×2
json.net ×2
.net ×1
.net-core ×1
asp.net-2.0 ×1
datetime ×1
javascript ×1
knockout.js ×1
linq ×1