如何从字符串中拆分单个数据..?

far*_*med -5 .net c# split webclient windows-phone-7

我想从字符串中获取单个数据,任何人都可以帮助我单独分割ID内容.我的代码存储结果值:

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)
{
    try
    {
        MessageBox.Show("Success..");
        MessageBox.Show(e.Result);
        string res=(string)e.Result;//here res contains my data..
        //need to get ID value alone.
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

Abb*_*bas 6

使用json2csharp为您的JSON字符串创建一个C#类.JSON的简化示例:

public class RootObject
{
    public string name { get; set; }
    public string description { get; set; }
    public string timezone { get; set; }
    public string id { get; set; }
}
//You can make the id of type int if you want
Run Code Online (Sandbox Code Playgroud)

使用JSON的最快方法是使用Json.NET.下载zip并使用文件夹中的dll Portable40,这是与Windows Phone 7兼容的版本.参考项目中的dll,然后您的代码应该类似于:

string jsonString = "{\"name\":\"Qwer\", \"description\":\"\", \"timezone\":\"UTC\", \"id\":\"2912\"}";
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonString);
int id = Convert.ToInt32(obj.id);
//value of id: 2912
Run Code Online (Sandbox Code Playgroud)