在Windows应用程序中解析JSON数据

本文关键字:JSON 数据 Windows 应用程序 | 更新日期: 2023-09-27 18:04:47

假设这是我通过API接收到的JSON数据:

[
    {
        "id": "1",
        "title": "Test_rom",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.1.png"
    },
    {
        "id": "2",
        "title": "Jewelry",
        "subtitle": "",
        "icon": "http://lpl.info/Admin/upload/cat.2.png"
    },
    {
        "id": "3",
        "title": "Jackets",
        "subtitle": "All sizes available",
        "icon": "http://lpl.info/Admin/upload/cat.3.png"
    }
]

我创建了一个名为"RootObject"的类:

public class RootObject {
    public string id { get; set; }
    public string title { get; set; }
    public string subtitle { get; set; }
    public string icon { get; set; }
}

我只想从数组中提取"title"键的值。我怎样才能做到呢?

我尝试了以下代码,但我在"编码"字中出现错误:

var result = await response.Content.ReadAsStringAsync();
RootObject TotalList = new RootObject();
RootObject childlistonly = new RootObject();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
DataContractJsonSerializer ser = new DataContractJsonSerializer(TotalList.GetType());
TotalList = ser.ReadObject(ms) as RootObject;
string category = "";
foreach(var d in TotalList.title) {
    category = category + " " + d.ToString() + "'n";
}
ResultsText.Text = category;

我使用了下面的代码,但是它给出了一个错误:

var titlevariable = myobj.title;
Textblock.Text = titlevariable; // (under a click button method)...

在Windows应用程序中解析JSON数据

试试这个

foreach(var d in TotalList) {
    category = category + " " + d.title.ToString() + "'n";
}

@kickaha..private async void Butoon_Click(对象发送方,RoutedEventArgs e)

    {
        var client = new HttpClient();
        var uri = new Uri("http://www.blabla.com/alliance/App/mobilelogin.php?KG=MA&EM="+Textboxname.Text+"&PA="+password.Password);
        var response = await client.GetAsync(uri);
        var result = await response.Content.ReadAsStringAsync();
        RootObject TotalList = new RootObject();
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
        RootObject resu = (RootObject)ser.ReadObject(ms);
       NavigationContext nav = new NavigationContext()
        {
            ID = resu.USERID.ToString(),
            Name = resu.NAME.ToString(),
            Email = resu.EMAIL.ToString()
      }
        SaveFile();
        RestoreFile(); 
         }
    private async void SaveFile()
    {
        StorageFile userdetailsfile = await ApplicationData.Current.LocalFolder.CreateFileAsync("UserDetails", CreationCollisionOption.ReplaceExisting);
        IRandomAccessStream raStream = await userdetailsfile.OpenAsync(FileAccessMode.ReadWrite);
        using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
        {
            // Serialize the Session State.
            DataContractSerializer serializer = new DataContractSerializer(typeof(NavigationContext));
            serializer.WriteObject(outStream.AsStreamForWrite(), nav);
            await outStream.FlushAsync();
        }  
    }
    private async void RestoreFile()
    {
        StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("UserDetails");
       if (file == null) return;
       IRandomAccessStream inStream = await file.OpenReadAsync();
        // Deserialize the Session State.
        DataContractSerializer serializer = new DataContractSerializer(typeof(NavigationContext));
        var StatsDetails = (NavigationContext)serializer.ReadObject(inStream.AsStreamForRead());
        inStream.Dispose();   
        string hu = StatsDetails.Name + "'n" + StatsDetails.Email;
        //  UserName.Text = "Welcome " + StatsDetails.Name;
        // UserProfileId = StatsDetails.Id;
        resultblock.Text = hu; 
    }

}

public class RootObject
{
    public string USERID { get; set; }
    public string EMAIL { get; set; }
    public string NAME { get; set; }
    public string FIRST_NAME { get; set; }
    public string LAST_NAME { get; set; }
    public string CITY { get; set; }   
}
public class NavigationContext
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Email { get; set; }
}

}

我在Restorefile()方法的"stream"属性中获得空值…