JSON parsing do obiektu klasy

0

Witajcie,

Próbuje odczytać ponizszy JSON do obiektu klasy:

{
	"Title": "Sample job",
	"Project id": "Sample project ID",
	"File renaming": "true",
	"Fields": {
		"Field1": {
			"Enabled": "true",
			"Name": "Name",
			"Sticky": "false",
			"Required": "true",
			"Filename": "true"
		},
		"Field2": {
			"Enabled": "false",
			"Name": "",
		 	"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field3": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field4": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field5": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field6": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field7": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			},
		"Field8": {
			"Enabled": "false",
			"Name": "",
			"Sticky": "false",
			"Required": "false",
			"Filename": "false"
			}
	    }
}

Obiekt klasy wygląda następująco:

public class jsonFileSettings
    {
        public string title { get; set; }
        public string projectID { get; set; }
        public bool fileRenaming { get; set; }
        public string[,] fields = new string[8, 5];
    }

Metoda deserializacji wygląda tak:

string jsonFileContent = File.ReadAllText(this.jsonPath);
var serializer = new JavaScriptSerializer();
var deserializedObject = serializer.Deserialize<jsonFileSettings>(jsonFileContent);

Niestety wszystkie wartosci nie sa odczytane (wszystko puste z wyjątkiem FileRenaming, które ustawia się na false) - nie mam zielonego pojęcia jak to rozwiązać- pomoże ktoś?

0

Sprawdz co masz w this.jsonPath

0

Chyba aż taki mądry deserializer nie jest. Musisz mu powiedzieć jak pole się nazywa.

public class jsonFileSettings
    {
        [JsonProperty("Title")]
        public string title { get; set; }
        [JsonProperty("Project id")]
        public string projectID { get; set; }
        [JsonProperty("File renaming")]
        public bool fileRenaming { get; set; }
        [JsonProperty("Fields")]
        public string[,] fields = new string[8, 5];
    }

Co do tego ostatniego nie jestem pewny czy to zadziała. Czy przypadkiem Fields nie powinien być arrayem?

1

@AdamWox:

Słownikiem

1

Mi to działa bez problemu. Podążałem według tematu: https://stackoverflow.com/questions/46355071/deserializing-a-list-of-objects-with-different-names-in-json-net

class Program
    {
        public class Field
        {
            public string Enabled { get; set; }
            public string Name { get; set; }
            public string Sticky { get; set; }
            public string Required { get; set; }
            public string Filename { get; set; }
        }
        public class Settings
        {
            [JsonProperty("Title")]
            public string Title { get; set; }
            [JsonProperty("Project id")]
            public string ProjectID { get; set; }
            [JsonProperty("File renaming")]
            public string FileRenaming { get; set; }
            [JsonProperty("Fields")]
            public Dictionary<string, Field> Fields { get; set; }
        }
        static void Main(string[] args)
        {
            string json = File.ReadAllText("json.txt");
            Settings settings = JsonConvert.DeserializeObject<Settings>(json);
            foreach(var field in settings.Fields)
                Console.WriteLine(field.Key + ": " + field.Value.Name);
            Console.ReadLine();
        }
        
    }
0

Działa w ten sposób. Dziękuję za pomoc. Teraz bede musial sie uporac z serializacja takich samych danych.

1 użytkowników online, w tym zalogowanych: 0, gości: 1