在本文中,我们将给您介绍关于LaravelHTTP测试使用getJson+assertJsonStructure对象数组的详细内容,并且为您解答laravel获取post参数的相关问题,此外,我们还将
在本文中,我们将给您介绍关于Laravel HTTP 测试使用 getJson + assertJsonStructure 对象数组的详细内容,并且为您解答laravel获取post参数的相关问题,此外,我们还将为您提供关于.NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化、c# – DataContractJsonSerializer和JsonConvert给出不同的结果、c# – 使用DataContractJsonSerializer设置JSON对象root、com.alibaba.fastjson JSONObject toJSONString 时出现数组转换错误的知识。
本文目录一览:- Laravel HTTP 测试使用 getJson + assertJsonStructure 对象数组(laravel获取post参数)
- .NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化
- c# – DataContractJsonSerializer和JsonConvert给出不同的结果
- c# – 使用DataContractJsonSerializer设置JSON对象root
- com.alibaba.fastjson JSONObject toJSONString 时出现数组转换错误
Laravel HTTP 测试使用 getJson + assertJsonStructure 对象数组(laravel获取post参数)
data
是一个 array
,因此,要断言嵌套对象的结构,我们需要使用 *
通配符:
$response->assertJsonStructure([
'data' => [
'*' => [
'createdAt','endAt','name','startAt','startedAt','status','updatedAt','range' => [
'max','min','type'
],]
]
]);
.NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化
InASP.NET AJAX Extensions v1.0 for ASP.NET 2.0 there is the JavaScriptSerializer class that provides JSONserialization and deserialization functionality. However,in .NET 3.5 the JavaScriptSerializer has been marked obsolete. The new object to use for JSON serialization in .NET 3.5 is the DataContractJsonSerliaizer object. I'm still new to the DataContractJsonSerializer,but here's a summary of what I've learned so far...
Object to Serialize
Here's a simple Person object with First Name and Last Name properties.
public class Person
{
public Person() { }
public Person(string firstname,string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
Now in orderto serialize our objectto JSONusing the DataContractJsonSerializer wemust either mark it with the Serializable attribute or the DataContract attribute. If we mark the class with the DataContract attribute,then we must mark each property we want serialized with the DataMember attribute.
/// Marked with the Serializable Attribute
[Serializable]
public class Person
{
public Person() { }
public Person(string firstname,string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
/// Marked with the DataContact Attribute
[DataContract]
public class Person
{
public Person() { }
public Person(string firstname,string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
Serialization Code
Jere's the most basic code to serialize our object to JSON:
Person myPerson = new Person("Chris","Pietschmann");
/// Serialize to JSON
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms,myPerson);
stringjson = Encoding.Default.GetString(ms.ToArray());
Our resulting JSON looks like this:
/// Result of Person class marked as Serializable
{"<FirstName>k__backingField":"Chris","<LastName>k__backingField":"Pietschmann"}
/// Result of Person class marked as DataContract with
/// each Property marked as DataMember
{"FirstName":"Chris","LastName":"Pietschmann"}
As you can see the first serialization with the class markedwith theSerializable attribute isn't quite what we were expecting,but is still JSON.This serialization actually isn't compatible with the client-side JSON Serializer in ASP.NET AJAX.
As you can see the second serialization with the class marked with the DataContract attribute is exactly what we were expecting,and is the same JSON that the old JavaScriptSerializer object would have generated. This is the method of JSON serialization using the DataContractJsonSerializer that you'll need to do if you are going to pass the resulting JSON down to the client to be consumed with ASP.NET AJAX.
Deserialization Code
Here's the most basic code to deserialize our object from JSON:
Person myPerson = new Person();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType());
myPerson= serializer.Readobject(ms) as Person;
ms.Close();
Controlling the property names in the resulting JSON
When using the DataContract and DataMember attributes,you can tell the DataMember attribute the specific name you want that property to have within the JSON serialization by setting its "Name" named parameter.
Here's an example that will give the name of "First" to the "FirstName" property within the JSON serialization:
[DataMember(Name = "First")]
public string FirstName { get; set; }
The resulting JSON looks like this:
Here's the code for some Helper methods using Generics to do all the dirty work for you
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public class JSONHelper
{
public static string Serialize<T>(T obj)
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms,obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
return retVal;
}
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.Readobject(ms);
ms.Close();
return obj;
}
}
/// Our Person object to Serialize/Deserialize to JSON
[DataContract]
public class Person
{
public Person() { }
public Person(string firstname,string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
/// Sample code using the above helper methods
/// to serialize and deserialize the Person object
Person myPerson = new Person("Chris","Pietschmann");
// Serialize
string json = JSONHelper.Serialize<Person>(myPerson);
// Deserialize
myPerson = JSONHelper.Deserialize<Person>(json);
What Assembly References does my application need for this?
From the namespace that containsDataContractJsonSerializer you can probably tell that you need to add a reference to the System.Runtime.Serialization assembly. However,you also need to add a reference to the System.ServiceModel.Web assembly.
c# – DataContractJsonSerializer和JsonConvert给出不同的结果
using (MemoryStream memoryStream = new MemoryStream()) { DataContractJsonSerializer dataContractSerializer = new DataContractJsonSerializer(typeof(Message),this.kNowTypes); dataContractSerializer.WriteObject(memoryStream,message); byte[] byteArray = memoryStream.ToArray(); memoryStream.Close(); return byteArray; }
当我将byteArray转换为字符串时,结果如下所示:
{ “__type”: “登录:#Project.ProjectName.sockets”,“密码”: “F9AAD6B7CFBD2A756101”,“用户名”: “用户名”}
这个结果对我的服务器有意义.
但是,由于某些字符问题,我想更改此代码.
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)); return byteArray;
现在我将byteArray转换为字符串结果,如:
{ “用户名”: “用户名”,“密码”: “F9AAD6B7CFBD2A756101”}
我也试过使用JsonSerializerSettings
settings = new JsonSerializerSettings(); settings.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterassemblyStyle.Full; settings.TypeNameHandling = TypeNameHandling.Objects;
结果是
{“$type”:“Project.ProjectName.sockets.Login,ProjectName”,“用户名”:“用户名”,“密码”:“F9AAD6B7CFBD2A756101”}
DataContractJsonSerializer和JsonConvert之间有什么区别,使用JsonConvert可以获得相同的结果.
解决方法
DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings() { EmitTypeinformation = EmitTypeinformation.Never };
c# – 使用DataContractJsonSerializer设置JSON对象root
例如,这是一个类:
[DataContract] public class Person { public Person() { } public Person(string firstname,string lastname) { this.FirstName = firstname; this.LastName = lastname; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } }
当它使用…序列化时
public static string Serialize<T>(T obj) { Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms,obj); string retVal = Encoding.Default.GetString(ms.ToArray()); ms.dispose(); return retVal; }
生成的JSON字符串如下所示:
{"FirstName":"Jane","LastName":"McDoe"}
有没有办法让序列化器前置一些值?
例如:
{Person: {"FirstName":"Jane","LastName":"McDoe"}}
当然,我可以简单地更改我的Serialize方法来包装返回的JSON字符串,例如:
string retVal = "{Person:" + Encoding.Default.GetString(ms.ToArray()) + "}";
但我想知道是否有某种方法告诉序列化程序添加它? DataContract属性上的namespace属性似乎没有帮助.
解决方法
public class StackOverflow_7930629 { [DataContract] public class Person { public Person() { } public Person(string firstname,string lastname) { this.FirstName = firstname; this.LastName = lastname; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } } public static string Serialize<T>(T obj) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T),typeof(T).Name); MemoryStream ms = new MemoryStream(); XmlDictionaryWriter w = JsonReaderWriterFactory.CreateJsonWriter(ms); w.WriteStartElement("root"); w.WriteAttributeString("type","object"); serializer.WriteObject(w,obj); w.WriteEndElement(); w.Flush(); string retVal = Encoding.Default.GetString(ms.ToArray()); ms.dispose(); return retVal; } public static void test() { Console.WriteLine(Serialize(new Person("Jane","McDoe"))); } }
正如其中一条评论中所提到的,使用JSON和DataContractJsonSerializer并不是太友好了.一些JSON特定的库(如JSON.NET或JsonValue类型(nuget包JsonValue))可以让您的生活更轻松.
com.alibaba.fastjson JSONObject toJSONString 时出现数组转换错误
JSONObject.toJSONString(vo); 改为 JSONObject.toJSON(templateCreateVo).toString();
今天关于Laravel HTTP 测试使用 getJson + assertJsonStructure 对象数组和laravel获取post参数的介绍到此结束,谢谢您的阅读,有关.NET 3.5: 使用DataContractJsonSerializer进行JSON 序列化、c# – DataContractJsonSerializer和JsonConvert给出不同的结果、c# – 使用DataContractJsonSerializer设置JSON对象root、com.alibaba.fastjson JSONObject toJSONString 时出现数组转换错误等更多相关知识的信息可以在本站进行查询。
本文标签: