'Return JSON string in a normal way
From one method I'm getting string like that one:
"{\n \"Name\": \"Next steps for pathway activities\",\n \"Options\": [\n {\n \"Name\": \"Show next steps\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"otoscopy\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Treatment cannot continue\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"treatment\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Refer to GP\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"refer_to_gp\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"New Wax appointment\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"new_wax_appointment_otoscopy\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"Continue treatment\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"pathway-activities\",\n \"Group\": \"treatment\"\n }\n ]\n}"
How can I parse it to a normal format? (P.S. It is not required to be as a string as a result, it also can be some dynamic object)
Solution 1:[1]
If you want to have an object and perform operations on this object in the future you can use Newtonsoft.Json NuGet package.
Just create classes that reflects your json like this:
class MyObject
{
public string Name { get; set; }
public IEnumerable<Option> Options { get; set; }
}
class Option
{
public string Name { get; set; }
public string ActionType { get; set; }
public string Key { get; set; }
public string Group { get; set; }
}
And try deserialize this json:
var obj = JsonConvert.DeserializeObject<MyObject>(jsonStr);
Solution 2:[2]
Like this you will get an object
let myStr = "{\n \"Name\": \"Next steps for pathway activities\",\n \"Options\": [\n {\n \"Name\": \"Show next steps\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"otoscopy\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Treatment cannot continue\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"treatment\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Refer to GP\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"refer_to_gp\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"New Wax appointment\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"new_wax_appointment_otoscopy\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"Continue treatment\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"pathway-activities\",\n \"Group\": \"treatment\"\n }\n ]\n}";
let myObj = JSON.parse(myStr);
console.log(myObj);
The output will be:
"Name": "Next steps for pathway activities",
"Options": [
{
"Name": "Show next steps",
"ActionType": "PathSelector",
"Key": "otoscopy",
"Group": "pathway-activities"
},
{
"Name": "Treatment cannot continue",
"ActionType": "Navigation",
"Key": "treatment",
"Group": "pathway-activities"
},
{
"Name": "Refer to GP",
"ActionType": "PathSelector",
"Key": "refer_to_gp",
"Group": "treatment"
},
{
"Name": "New Wax appointment",
"ActionType": "PathSelector",
"Key": "new_wax_appointment_otoscopy",
"Group": "treatment"
},
{
"Name": "Continue treatment",
"ActionType": "Navigation",
"Key": "pathway-activities",
"Group": "treatment"
}
]
}
Solution 3:[3]
You can do like this take you json string
var myStr = "{\n \"Name\": \"Next steps for pathway activities\",\n \"Options\": [\n {\n \"Name\": \"Show next steps\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"otoscopy\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Treatment cannot continue\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"treatment\",\n \"Group\": \"pathway-activities\"\n },\n {\n \"Name\": \"Refer to GP\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"refer_to_gp\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"New Wax appointment\",\n \"ActionType\": \"PathSelector\",\n \"Key\": \"new_wax_appointment_otoscopy\",\n \"Group\": \"treatment\"\n },\n {\n \"Name\": \"Continue treatment\",\n \"ActionType\": \"Navigation\",\n \"Key\": \"pathway-activities\",\n \"Group\": \"treatment\"\n }\n ]\n}";
var _obj = JSON.parse(myStr);
console.log(_obj);
OutPut in Image
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | adrianMoskal |
Solution 2 | Jeremy Caney |
Solution 3 | Kamran Khan |