我正在阅读本书,但我从System.Web.Script.Serialization命名空间获取JavaScriptSerializer,因为它似乎不可用?
我找到了一些答案,在大型响应的上下文中解决了这个问题,但是对于我的大型请求的上下文没有.将大文件发布到操作方法时,我收到以下异常:
使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过maxJsonLength属性上设置的值.
这是在我的动作方法被击中之前,所以在我的回复中告诉我设置更高的json大小的帖子的丰富在这里都是无用的.
堆栈跟踪是:
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String input)
at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ValueProviderFactoryCollection.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
at System.Web.Mvc.ControllerBase.get_ValueProvider()
at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.Begin(AsyncCallback callback, …Run Code Online (Sandbox Code Playgroud) 在早期版本的ASP.NET中,System.Web.Script.Serialization.JavaScriptSerializer可用.用法:
Dictionary<string, object> sample = (Dictionary<string, object>)new JavaScriptSerializer().DeserializeObject("...");
Run Code Online (Sandbox Code Playgroud)
在ASP.NET 5 RC 1中,JavaScriptSerializer默认情况下似乎不可用.
JavaScriptSerializerASP.NET 5 RC 1?System.Web新版本不提供,有没有替代方案?c# serialization javascriptserializer asp.net-core-mvc asp.net-core
在我的C#代码中,我试图反序列化具有100多个属性(复杂,原始,派生的)的JSON,但出现错误Cannot convert null to a value type.
尽管我最终通过手动故障排除知道了哪个属性导致了问题。
但是,有什么方法可以让我简单地知道JSON或Result_TYPE property or properties(一次完成),从而导致此问题?
我尝试调查detail windowException,但我只能知道datatype。就我而言,它null试图转换为boolean。,但找不到属性名称。
例如:我的JSON
{
"customerData":
{
//... other json data
"someClass":{
"someClassProp1":"prop1Value",
"someClassProp2":"prop2Value"
},
"isExistData":null,
"someOtherClass":null
//... some other json data
}
}
Run Code Online (Sandbox Code Playgroud)
并且Result_TYPE为:
Public class CustomerData
{
// Other properties
public SomeClass someClass:
public bool isExistData;
public SomeOtherClass someOtherClass:
// some Other properties
}
Run Code Online (Sandbox Code Playgroud)
我正在使用
JavaScriptSerializer().Deserialize<T>(jsonString);
在上面的示例中:我将如何知道该属性isExistData将导致反序列化错误,因为属性类型为boolean,传入数据为null。[当然除了手动调试,因为可能有100多个属性]
任何人,如果知道找到确切财产的更好方法?
我正在将具有其他类型和列表的许多属性的复杂对象序列化为JSON格式,但问题在于DateTime属性.我用JavascriptSerializer获得了纪元时间(而不是mm/dd/YYYY).
有没有什么方法可以获得mm/dd/YYYY:HH.MM.SS形式的日期时间而不修改我序列化的对象的类定义.
我已经返回了一个Json(myObj)动作结果.myObj是Badge类型
Badge中唯一可能导致序列化程序循环的两个对象是:
public class Badge
{
public Badge() { }
public Badge(String Name, String Description)
{
this.Name = Name;
this.Description = Description;
}
[ScriptIgnore]
public virtual BadgeType BadgeType { get; set; }
[ScriptIgnore]
public virtual ICollection<User> Users { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string PrerequisiteCriteriaRef { get; set; }
//PrerequisiteID
public static Badge CreateForSeeder(BaseDBContext db, String Name, String Description, int …Run Code Online (Sandbox Code Playgroud) 您如何使用JavaScriptSerializer和自定义JavaScriptConverter序列化和反序列化其键为JSON 整数的字典?
对于那些不知道的人,JavaScripSerializer无法开箱即用.
请注意,我对需要在序列化之前转换字典或使用其他序列化程序的解决方案感兴趣(如果有的话,您可能会看到此帖子).
更新:为了消除任何歧义,我没有问题,密钥是作为JSON中的字符串生成的(因此1将变为"1").
我有一个带有嵌套对象的JSON数组,代表一个菜单,如下所示:
[
[
{
"name": "Item 1",
"id": 1
},
{
"name": "Item 2",
"id": 2,
"children": [
[
{
"name": "Item 21",
"id": 21
}
]
]
},
{
"name": "Item 3",
"id": 3,
"children": [
[
{
"name": "Item 31",
"id": 31,
"children": [
[
{
"name": "Item 311",
"id": 311
},
{
"name": "Item 312",
"id": 312
}
]
]
},
{
"name": "Item 32",
"id": 32
},
...
Run Code Online (Sandbox Code Playgroud)
我想使用JavaScriptSerializer对其进行反序列化.我有一些代码,如下所示,但无法正常工作.
var serializer = new JavaScriptSerializer();
var objects …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个包含一个容器和一个数组的JSON字符串.我可以通过使用stringbuilder来做到这一点,但我试图找到一个更好的方法来获取JSON字符串; 我想要:
{ "message":{ "text":"test sms"},"endpoints":["+9101234"]}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
string json = new JavaScriptSerializer().Serialize(new
{
text = "test sms",
endpoints = "[dsdsd]"
});
Run Code Online (Sandbox Code Playgroud)
输出是:
{"text":"test sms","endpoints":"[dsdsd]"}
Run Code Online (Sandbox Code Playgroud)
如何获得所需格式的任何帮助或建议?
当我反序列化它的工作列表时,但是当我反序列化为具有列表类型的对象时,它会出错.知道如何让它工作吗?
页面名称:testjson.aspx
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace Web.JSON
{
public partial class testJson : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string json = "[{\"SequenceNumber\":1,\"FirstName\":\"FN1\",\"LastName\":\"LN1\"},{\"SequenceNumber\":2,\"FirstName\":\"FN2\",\"LastName\":\"LN2\"}]";
//This work
IList<Person> persons = new JavaScriptSerializer().Deserialize<IList<Person>>(json);
//This error
//People persons = new JavaScriptSerializer().Deserialize<People>(json);
Response.Write(persons.Count());
}
}
class Person
{
public int SequenceNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
class People : List<Person>
{
public People()
{
} …Run Code Online (Sandbox Code Playgroud) 我使用JavaScriptSerializerClass来对Json进行serilize和Deserilize.
我know有一个json.net图书馆.
但我的问题是:
我还可以使用JavaScriptSerializer 类来逃避我的json字符串吗?
或者我应该自己做?如果是这样我应该这样做encodeURIComponent?
我创建了一个通用函数,它可以检索JSON并解析它:
public IList<Object> RetrieveView(string result)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
IList<Object> values = ser.Deserialize< IList<Object> >(result);
return values;
}
Run Code Online (Sandbox Code Playgroud)
我有很多使用它的类的问题.例如,当我尝试使用此:
IList<Receipt> receipts = (IList<Receipt>)RetrieveView(result);
Run Code Online (Sandbox Code Playgroud)
我明白了 InvalidCastException Unable to cast object of type 'System.Collections.Generic.List[System.Object]' to type 'System.Collections.Generic.IList[Receipt]'.
无论如何要保持我的功能Generic并能够将它投射到我的所有类/模型?
我收到了错误Invalid JSON primitive.所以我一直在简化我的JSON字符串,直到空数组:[]我甚至检查字符串是否正确,并且长度为2.
我相信这[]应该适用于一系列的任何东西?
我的代码是
jsonString = "[]";
var v = new JavaScriptSerializer();
Felagi[] felagar = (Felagi[])v.Deserialize<Felagi[]>("jsonString");
Run Code Online (Sandbox Code Playgroud)
对于它可能值得的东西,我尝试反序列化的类是
public class Felagi
{
public String firstName { get; set; }
}
Run Code Online (Sandbox Code Playgroud) c# ×10
json ×9
.net ×2
asp.net-mvc ×2
asp.net ×1
asp.net-core ×1
casting ×1
datetime ×1
dictionary ×1
object ×1
xamarin.ios ×1