You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to serialize an object into json. I get an empty string. below is the code i used.
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace test
{
public class AdbEmulator
{
//needs to be public otherwise wont work
public string name;
internal string attached;
}
public class AndroidJson
{
public AndroidJson()
{
AddData();
}
public void AddData()
{
AdbEmulator adb1 = new AdbEmulator() { name = "emulator-5554", attached = "ll" };
emulators.Add(adb1);
string jsonAdb = System.Text.Json.JsonSerializer.Serialize<AdbEmulator>(adb1);
string newtonjson = Newtonsoft.Json.JsonConvert.SerializeObject(adb1);
}
}
Program.cs
static void Main(string[] args)
{ AndroidJson aj = new AndroidJson(); }
I'm not sure why newtonsoft works and the microsoft one does not.
i'v'e tried the code in an dotnet core 3.1 project and windows application ( including the nuget package for that project).
The text was updated successfully, but these errors were encountered:
@Greypuma, if you turn your fields into properties it should work as expected. Please re-open if you still see issues.
Thanks.
using System.Text.Json;using System.Text.Json.Serialization;namespacetest{publicclassAdbEmulator{//needs to be public otherwise wont workpublicstringname{get;set;}internalstringattached;}}
I was trying to serialize an object into json. I get an empty string. below is the code i used.
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace test
{
public class AdbEmulator
{
//needs to be public otherwise wont work
public string name;
internal string attached;
Program.cs
static void Main(string[] args)
{ AndroidJson aj = new AndroidJson(); }
Output
jsonAdb = {}
newtonjson ={"name":"emulator-5554"}
I'm not sure why newtonsoft works and the microsoft one does not.
i'v'e tried the code in an dotnet core 3.1 project and windows application ( including the nuget package for that project).
The text was updated successfully, but these errors were encountered: