Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace $ when converting JSON object to c# with out actually changing the name of that particular field in which $ exists?

Tags:

json

c#-4.0

When I'm converting Json objects to c#, I got an issue In which my Json has fields with $ symbol(ex: $t).But c# doesn't accept fields with special characters. If i try to replace $ with any other letters in my c# code, I'm unable to get data from 3rd party because of the change in naming.

How can I solve this issue?

Json string:

"author": [(1)
{
"name": {
"$t": "theabctv"
},-
"uri": {
 $t": "http://gdata.abc.com/feeds/api/users/theabctv"
},-
"yt$userId": {
"$t": "tCUABCCT7wYG1PMCpw"
}-
}-
],-

C# code:-

public class Author2

{

public Name2 name { get; set; }
public Uri2 uri { get; set; }
public YtUserId __invalid_name__yt$userId { get; set; }
}
public class Name2
{
public string __invalid_name__$t { get; set; }
}

public class Uri2
{
public string __invalid_name__$t { get; set; }
}

public class YtUserId
{
public string __invalid_name__$t { get; set; }
}
like image 480
user1881554 Avatar asked Nov 03 '22 09:11

user1881554


1 Answers

There is no way to declare property names with symbols in NET framework, meaning that you cant have isomorphism between the JSON objects and the C# objects without parsing the JSON data. You could replace all $ symbol with any given string (carefully chosen), manage the data in the code behind and when you need to send JSON data of the object apply the inverse replacement.

like image 138
a0viedo Avatar answered Nov 15 '22 06:11

a0viedo