Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there! I am dealing with a non standard json string, here:
string a = "[[\"test, test\",\"{\\\"prop1\\\":1,\\\"prop2\\\":\\\"test\\\",\\\"prop3\\\":\\\"test\\\"}\"],[\"test2, test2\",\"{\\\"prop1\\\":2,\\\"prop2\\\":\\\"test2\\\",\\\"prop3\\\":\\\"test2\\\"}\"]]";

And trying to parse this string to an object. Tried, like, everything :( Nothing works. I am not really sure, if I understand this json structure correctly. I would really appreciate, if someone could help me with this. Here is formatted JSON:
[
	[
		"test",
		{
			"prop1": 1,
			"prop2": "test",
			"prop3": "test"
		}
	],
	[
		"test2",
		{
			"prop1": 2,
			"prop2": "test2",
			"prop3": "test2"
		}
	]
]

But I still do not understand its structure.

What I have tried:

I have created some objects for this json, as far as I can understand its structure. Here they are:
class Details
{
    public int prop1 { get; set; }
    public string prop2 { get; set; }
    public string prop3 { get; set; }
}
class Name
{
    public string FullName { get; set; }
}
class Class
{
    public Name FullName { get; set; }
    public Details Details { get; set; }
}

And trying to deserialize this way:
string a = "[[\"test, test\",\"{\\\"prop1\\\":1,\\\"prop2\\\":\\\"test\\\",\\\"prop3\\\":\\\"test\\\"}\"],[\"test2, test2\",\"{\\\"prop1\\\":2,\\\"prop2\\\":\\\"test2\\\",\\\"prop3\\\":\\\"test2\\\"}\"]]";
List< Class > obj = JsonConvert.DeserializeObject<List<Class>>(a);
Posted
Updated 23-Sep-17 2:34am
v2
Comments
Richard MacCutchan 23-Sep-17 7:32am    
If you get rid of all those backslash characters and reformat the remaining text properly it will be easy to figure out the structure. Maybe if you explained what problem you have then people would be able to make suggestions.
csrss 23-Sep-17 7:35am    
Here is formatted json:
[
[
"test",
{
"prop1": 1,
"prop2": "test",
"prop3": "test"
}
],
[
"test2",
{
"prop1": 2,
"prop2": "test2",
"prop3": "test2"
}
]
]

I still got no idea, what the object structure is.

Alright, found a way:
JArray array = JArray.Parse(a);
foreach(JToken jToken in array)
{
    JToken[] innerArray = jToken.ToArray();
    string fullName = innerArray[0].Value<string>();
    Details details = JObject.Parse(innerArray[1].Value<string>()).ToObject<Details>();
}
 
Share this answer
 
THis article will show you an easier way: Working with JSON in C# & VB[^]
 
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