Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a json data in particular format,i need to change the data format to use in my code.How to change it in c# code or some other thing?

What I have tried:

JSON
[

    "concepts": {
         "http://dbpedia.org/resource/Chuckles_(G.I._Joe)": {
           "SurfaceForms": [
             {
               "Score": 0.9460024,
               "String": "CHUCKLES",
               "Offset": 3
             }
           ],
           "Types": [ "" ],
           "Support": 52
         }
       },
"concepts": {
         "http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn": {
           "SurfaceForms": [
             {
               "Score": 1.0,
               "String": "Sithi",
               "Offset": 2
             }
           ],
           "Types": [ "" ],
           "Support": 43
         }
       }

]


In this JSON data how to get Score inside a SurfaceForms.
Posted
Updated 7-Jan-18 21:34pm
Comments
Ram Nunna 8-Jan-18 3:08am    
you need to create proper classes in C#.
Please refer below link.
https://stackoverflow.com/questions/11260631/convert-json-into-class-object-in-c-sharp

This article will give you the tools and show you how to do what you are asking: Working with JSON in C# & VB[^]

Also, there is an error in your JSON data. It should be:
{

    "concepts": {
         "http://dbpedia.org/resource/Chuckles_(G.I._Joe)": {
           "SurfaceForms": [
             {
               "Score": 0.9460024,
               "String": "CHUCKLES",
               "Offset": 3
             }
           ],
           "Types": [ "" ],
           "Support": 52
         }
       },
"concepts": {
         "http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn": {
           "SurfaceForms": [
             {
               "Score": 1.0,
               "String": "Sithi",
               "Offset": 2
             }
           ],
           "Types": [ "" ],
           "Support": 43
         }
       }

}


And here is the C# classes generated by JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]:
C#
public class SurfaceForm
{

	[JsonProperty("Score")]
	public double Score { get; set; }

	[JsonProperty("String")]
	public string String { get; set; }

	[JsonProperty("Offset")]
	public int Offset { get; set; }
}

public class HttpDbpediaOrgResourceMemorySorrowAndThorn
{

	[JsonProperty("SurfaceForms")]
	public IList<SurfaceForm> SurfaceForms { get; set; }

	[JsonProperty("Types")]
	public IList<string> Types { get; set; }

	[JsonProperty("Support")]
	public int Support { get; set; }
}

public class Concepts
{

	[JsonProperty("http://dbpedia.org/resource/Memory,_Sorrow,_and_Thorn")]
	public HttpDbpediaOrgResourceMemorySorrowAndThorn HttpDbpediaOrgResourceMemorySorrowAndThorn { get; set; }
}

public class Result
{

	[JsonProperty("concepts")]
	public Concepts Concepts { get; set; }
}

To populate the classes, the above article link[^] will show you how.
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 8-Jan-18 4:09am    
5
You have two options:
1. Create a class that fits the JSON and use it (the easier way IMHO)
2. Use JObject[^] from Newtonsoft - it can contain any JSON without preparation...
 
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