我在使用api调用时遇到了json格式的问题。我需要这样的东西
{
"token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
"answers": [
{
"question": "Where are you from",
"ans": "t"
},
{
"question": "I am from tts",
"ans": "f"
}
]
}我有一系列名为“答案”的散列,这是我单独使用以下内容创建的
string json = JsonConvert.SerializeObject(account, Formatting.Indented);之后,我必须使用令牌,但使用相同的进程,我得到
{
"token": "87dd8f93-27ad-493c-8ab1-e75c50b8fb71",
"answers": [
"{\r\n
\"question\": \"Where are you from\",
\"ans\": \"t\"
}",
"{\r\n
\"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;
}需要帮助,谢谢
发布于 2014-02-28 15:13:08
我认为你想要的东西可以通过重新整理你的数据来实现。创建一个要序列化以返回的新对象
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);https://stackoverflow.com/questions/22098349
复制相似问题