我试图反序列化以下JSON,但我真的不知道如何使用JSON.net来完成工作.我正在使用C#和JSON.Net库.
我的JSON如下:
{
"found": 3,
"bounds": [
[
-43.54919,
172.62148
],
[
-43.54487,
172.63654
]
],
"features": [
{
"id": 15342454,
"centroid": {
"type": "POINT",
"coordinates": [
-43.54779,
172.62148
]
},
"bounds": [
[
-43.54779,
172.62148
],
[
-43.54779,
172.62148
]
],
"properties": {
"osm_element": "node",
"amenity": "toilets",
"synthesized_name": "Toilets",
"osm_id": "502884303"
},
"geometry": {
"type": "POINT",
"coordinates": [
-43.54779,
172.62148
]
},
"location": {
"county": "Canterbury",
"country": "New Zealand",
"road": "Sommerset Crescent",
"city": "Christchurch"
},
"type": "Feature"
},
{ …Run Code Online (Sandbox Code Playgroud) 如何在Java中序列化Serializable类的许多对象?
例如
public class Personne implements java.io.Serializable {
String nom;
String date;
Personne(String nom,String date)
{
this.nom=nom;
this.date=date;
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建许多objets:
Personne p1 = new Personne("X","XX");
Personne p2 = new Personne("Y","ZZ");
Personne p3 = new Personne("Z","ZZ");
Run Code Online (Sandbox Code Playgroud)
- > static function来序列化那些对象
- >反序列化的静态函数
完整代码:
package game2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
*
* @author ahmedJ
*/
public class Personne implements java.io.Serializable {
String nom;
String date;
Personne(String nom, String date) {
this.nom = nom;
this.date = date; …Run Code Online (Sandbox Code Playgroud) 我像这样序列化了一个名为GreenhouseControls的类:
public class GreenhouseControls extends Controller implements Serializable{
......
public void saveState() {
try{
// Serialize data object to a file
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("dump.out"));
out.writeObject(GreenhouseControls.this);
System.out.println(GreenhouseControls.errorcode); // Prints 1
out.close();
} catch (IOException e) {
}
}
......
}
Run Code Online (Sandbox Code Playgroud)
当GreenhouseControls对象被序列化时,全局静态变量'errorcode'被设置为1.
然后,我将GreenhouseControls类反序列化为:
public class GreenhouseControls extends Controller implements Serializable{
......
public class Restore extends Event {
.....
@Override
public void action() {
try {
FileInputStream fis = new FileInputStream(eventsFile);
ObjectInputStream ois = new ObjectInputStream(fis);
GreenhouseControls gc = (GreenhouseControls) …Run Code Online (Sandbox Code Playgroud) 我有一个User属于Authentication命名空间的类,如下所示:
namespace Authentication {
public class User
{
public string Name {get;set;}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用BinaryFormatter将类User的对象序列化为byte []并将其存储在数据库中.
User usr = new User();
usr.Name = "myself";
var usrValue = convertObjectToByteArr(usr);
//then store usrValue into db.
Run Code Online (Sandbox Code Playgroud)
一段时间后,类User具有新属性并移动到新命名空间: Authentication.Organization
namespace Authentication.Organization{
public class User
{
public string Name {get;set;}
public int Age {get;set;}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:如何将previous用户对象反序列化为current用户?
我会遇到异常:{"Unable to load type Authentication.User required for deserialization."}当我尝试反序列化时:
byte[] prevUsrValue= (byte[])helper.getPreviousUserValue();//read from database
User previousUser = convertByteArrToObject(prevUsrValue) as …Run Code Online (Sandbox Code Playgroud) 我有一个字节数组,我需要将其反序列化为几种对象类型。
该对象包含{float,short,int}。在Java中,我可以这样ObjectInputStream:
ObjectInputStream is;
is.readFloat()
is.readShort()
is.readInt()
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种用C#做到这一点的方法。
读取第一个x字节为float,下一个y字节为short,下一个z字节为int。
我的JSON看起来像这样 -
{"0abc34m": {"time": "13 Mar 17, 4:50:02 PM", "pd": "oscar"}}
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码对此进行反序列化 -
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JSONstring));
ms.Position = 0;
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Rootobject));
Rootobject myDataTypObj = (Rootobject)jsonSerializer.ReadObject(ms);
Run Code Online (Sandbox Code Playgroud)
我使用Visual Studio函数"粘贴JSON作为类"函数来生成这些类 -
public class Rootobject
{
public _00A462 _00a462 { get; set; }
}
public class _00A462
{
public string time { get; set; }
public string pd { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想访问JSON的"time"和"pd"成员.
JSON的第一部分是每次收到新的JSON字符串时都会更改的数字.
我没有错误但我的myDataTypObj具有_00A462的空值.
我只关心层次结构中二级的字段.我正确地接近这个问题,这里有什么问题?
我第一次尝试使用Philips Hue light api,但我对如何将json字符串反序列化为C#对象有一些疑问。我正在针对我正在使用的Xamarin.iOS应用进行尝试。
这是我的方法,可以从我周围获取灯光数据:
private string getLights()
{
var url = APIURL + APIKey + LightsEndPoint ;
var request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "GET";
using (var response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
Console.Out.WriteLine(
"Error fetching data. Server returned status code: {0}",
response.StatusCode);
using (var reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
if(string.IsNullOrWhiteSpace(content))
{
Console.WriteLine("Response contained empty body...");
}
else
{
Console.WriteLine("Response Body: \r\n {0}", content);
var items = JsonConvert.DeserializeObject <Light> …Run Code Online (Sandbox Code Playgroud) 我的团队现在已经有几个星期这个问题了,我们有点难过.善意和知识将优雅地收到!
使用嵌入式系统,我们试图序列化一个对象,通过Linux套接字发送它,在另一个进程中接收它,然后将它反序列化回原始对象.我们有以下反序列化功能:
/*! Takes a byte array and populates the object's data members */
std::shared_ptr<Foo> Foo::unmarshal(uint8_t *serialized, uint32_t size)
{
auto msg = reinterpret_cast<Foo *>(serialized);
return std::shared_ptr<ChildOfFoo>(
reinterpret_cast<ChildOfFoo *>(serialized));
}
Run Code Online (Sandbox Code Playgroud)
该对象已成功反序列化,可以从中读取.但是,当std::shared_ptr<Foo>调用返回的析构函数时,程序会出现段错误.Valgrind给出以下输出:
==1664== Process terminating with default action of signal 11 (SIGSEGV)
==1664== Bad permissions for mapped region at address 0xFFFF603800003C88
==1664== at 0xFFFF603800003C88: ???
==1664== by 0x42C7C3: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:149)
==1664== by 0x42BC00: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:666)
==1664== by 0x435999: std::__shared_ptr<ChildOfFoo, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (shared_ptr_base.h:914)
==1664== by 0x4359B3: std::shared_ptr<ChildOfFoo>::~shared_ptr() (shared_ptr.h:93)
Run Code Online (Sandbox Code Playgroud)
我们对任何建议都持开放态度!感谢您的时间 :)
我想要反序列化json对象.但对于"电子邮件",除了它之外还有括号.在Account类中,它不允许放置"[Email]"而只允许"Email".有人可以帮忙吗?谢谢.
类:
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
JSON:
{
'[Email]': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
}
Run Code Online (Sandbox Code Playgroud)
我如何反序列化:
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
Run Code Online (Sandbox Code Playgroud) 我有一本字典.
Dictionary<string, List<string>>
Run Code Online (Sandbox Code Playgroud)
我使用这一行序列化了这个对象;
var ss = JsonConvert.SerializeObject(objDic, Formatting.Indented);
Run Code Online (Sandbox Code Playgroud)
JSON是有效的!
{
"keywords a": [
"keywords analytics,",
"keywords abstract,",
"keywords amazon,",
"keywords anywhere,",
"keywords adwords,",
"keywords architecture,",
"keywords apa,",
"keywords app store,",
"keywords and seo,",
"keywords armor,"
],
"keywords b": [
"keywords book,",
"keywords bidding,",
"keywords broad match,",
"keywords blog,",
"keywords business,",
"keywords book pdf,",
"keywords by country,",
"keywords by industry,",
"keywords bridge,",
"keywords bidding strategy,"
],
"keywords c": [
"keywords creator,",
"reserved keywords c,",
"keywords c#,",
"keywords combo list,",
"keywords …Run Code Online (Sandbox Code Playgroud) deserialization ×10
c# ×7
json ×5
arrays ×2
java ×2
.net ×1
c++ ×1
class ×1
dictionary ×1
embedded ×1
list ×1
xamarin.ios ×1