Return JSON 正常方式的字符串
Return JSON string in a normal way
通过一种方法我得到了这样的字符串:
"{\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}"
如何将其解析为正常格式?
(P.S。结果不一定是字符串,也可以是动态对象)
这样你会得到一个对象
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);
输出将是:
"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"
}
]
}
你可以这样带你 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
以后如果你想拥有一个对象并对该对象进行操作可以使用Newtonsoft.JsonNuGet包。
只需创建反映您的 json 的 类,如下所示:
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; }
}
并尝试反序列化这个 json:
var obj = JsonConvert.DeserializeObject<MyObject>(jsonStr);
通过一种方法我得到了这样的字符串:
"{\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}"
如何将其解析为正常格式? (P.S。结果不一定是字符串,也可以是动态对象)
这样你会得到一个对象
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);
输出将是:
"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"
}
]
}
你可以这样带你 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
以后如果你想拥有一个对象并对该对象进行操作可以使用Newtonsoft.JsonNuGet包。
只需创建反映您的 json 的 类,如下所示:
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; }
}
并尝试反序列化这个 json:
var obj = JsonConvert.DeserializeObject<MyObject>(jsonStr);