我在 api 调用的 json 形成方面遇到问题。我需要这样的东西
{
"token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
"answers": [
{
"question": "Where are you from",
"one_answers": "t"
},
{
"question": "I am from tts",
"one_answers": "f"
}
]
}
我有一个名为答案的哈希数组,我分别用类似的东西制作
string json = JsonConvert.SerializeObject(account, Formatting.Indented);
之后,我必须使用令牌进行制作,但随后使用相同的过程,我得到了
{
"token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
"answers": [
"{rn
"question": "Where are you from",
"ans": "t"
}",
"{rn
"question": "I am from tts",
"ans": "f"
}"
]
}
public class Account
{
public string question { get; set; }
public string ans { get; set; }
}
之后
if (ansNo.IsChecked == true)
{
Account account = new Account
{
question = quizText.Text,
ans = "f"
};
string json = JsonConvert.SerializeObject(account, Formatting.Indented);
Globals.answers[counter] = json;
}
else
{
Account account = new Account
{
question = quizText.Text,
ans = "t"
};
string json = JsonConvert.SerializeObject(account, Formatting.Indented);
Globals.answers[counter] = json;
}
需要帮助谢谢
我认为你追求的东西可以通过重新调整你的数据来实现。创建一个要序列化以返回的新对象
public class MyJson{
public string token {get;set;}
public List<Account> answers {get;set;}
public MyJson(){
answers = new List<Account>();
}
}
创建新的 MyJson 对象并添加令牌
MyJson o = new MyJson { token = "87dd8f93-27ad-493c-8ab1-e75c50b8fb71" }
然后就像您已经添加到列表中的答案一样
Account account = new Account{
question = quizText.Text,
ans = "t"
};
o.answers.Add(account);
然后序列化整个东西并返回
return JsonConvert.SerializeObject(o, Formatting.Indented);