Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to deserialize two json strings to the obeject, in these two json strings there are similar attributes that cannot be ignored. So the properties in converted C # object must also remain. these two strings are the parameters in Json String of two Api Call. First API makes a folder and second API adds a file to this created folder, so the parameters and properties of json that are similar cannot be ignored and excluded .. I have put two classes that are converted from the json string into the main program The two are in a namespace, if the name of one of these namespace changes, there is a conflict in the code in the place where the second string is deserialized :

List<fileinsert> example2 = JsonConvert.DeserializeObject<List<fileinsert>>(fileinsert.Content);

under fileinsert there is a red line: "CS0118:" fileinsert "is" Variable "but is used like" Type ".


Here is the deserialed object from the second Json string:
C#
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Text.Json;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization;

namespace Api2
{
    public class Datum
    {
        public string internalName { get; set; }
        public string value { get; set; }
    }

    public class Typ
    {
        public string internalName { get; set; }
        public string value { get; set; }
    }

    public class Fields
    {
        public Datum Datum { get; set; }
        public Typ Typ { get; set; }
    }

    public class fileinsert
    {
        public string cabinet { get; set; }
        public string name { get; set; }
        public string objectTypeId { get; set; }
        public Fields fields { get; set; }
    }
}

How do I have to proceed so that both objects are desrialized and still the similar attributes are retained and all remain in one namespace.

I thank you in advance

here is the code and also the converted code from First Json String :
C#
<pre>List<folderinsert>example1= JsonConvert.DeserializeObject<List<folderinsert>>(folderinsert.Content);


C#
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Text.Json;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Text.Json.Serialization;

namespace Api1
{
    
    public class Datum
    {
        public string value { get; set; }
    }
   
    public class Fields
    {
        public Datum Datum { get; set; }
    }

    public class folderinsert
    {
        public string cabinet { get; set; }
        public string name { get; set; }
        public string objectTypeId { get; set; }
        public Fields fields { get; set; }
    }
}


What I have tried:

I tried to change the namespaces
Posted
Updated 19-Mar-21 1:43am
v2

1 solution

C#
List<fileinsert> example2 = JsonConvert.DeserializeObject<List<fileinsert>>(fileinsert.Content);
     class reference                                                        object reference

You have declared fileinsert as a class, but in the above line you are also referring to an object with the same name. You need to use different names for classes and objects.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900