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)
CodePudding user response:
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"
}
]
}```
CodePudding user response:
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);
CodePudding user response:
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