简体   繁体   English

如何将子对象从JArray放入ObservableCollection

[英]How to get child objects from a JArray into an ObservableCollection

I am developing an app for Windows Phone, where a ListBox shows data from a JSON file. 我正在为Windows Phone开发一个应用程序,其中的ListBox显示来自JSON文件的数据。 I'm using a JArray and I can display data according to an array position. 我正在使用JArray并且可以根据数组位置显示数据。 But what if I want to display all data from my JSON file (the file doesn't have static data, and the data may be modified later)? 但是,如果我想显示JSON文件中的所有数据(该文件没有静态数据,并且以后可能会修改数据)怎么办?

My JSON: 我的JSON:

[
    {
        "xId": "52",
        "result": {
            "type": "Basico.Bean.MunicipioClass.TMunicipio",
            "id": 1,
            "fields": {
                "FRefCount": 0,
                "FId": 52,
                "FNome": "Sumare",
                "FEstado": "SP",
                "FPais": "Brasil"
            }
        }
    },
    {
        "xId": "52",
        "result": {
            "type": "Basico.Bean.MunicipioClass.TMunicipio",
            "id": 1,
            "fields": {
                "FRefCount": 0,
                "FId": 52,
                "FNome": "Indaiatuba",
                "FEstado": "SP",
                "FPais": "Brasil"
            }
        }
    }
]

My Code: 我的代码:

InitializeComponent();

String text;


using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var readStream = new IsolatedStorageFileStream("json.html", FileMode.Open, FileAccess.Read, FileShare.Read, store))
using (var reader = new StreamReader(readStream))
{
    text = reader.ReadToEnd();
}
{
    try
    {


        DataContext = this;

        // Your JSON string
        string json = text;

        // Parse as JObject
        JArray jObj = JArray.Parse(json);


        // Extract what you need, the "fields" property
        JToken jToken = jObj[0]["result"]["fields"];

        // Convert as Fields class instance
        Fields fields = jToken.ToObject<Fields>();

        Items = new ObservableCollection<Fields>() { fields };
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public ObservableCollection<Fields> Items { get; set; }

public class Fields
{
    [JsonProperty(PropertyName = "FId")]
    public int FId { get; set; }

    public string FNome { get; set; }
    public string FEstado { get; set; }
    public string FPais { get; set; }
}

When i used "[0]" the return is Sumare SP : 当我使用“ [0]”时,返回的是Sumare SP

JToken jToken = jObj[0]["result"]["fields"];

When i used "[1]" the return is Indaiatuba SP : 当我使用“ [1]”时,返回的是Indaiatuba SP

JToken jToken = jObj[1]["result"]["fields"];

I need it like this: 我需要这样:

Sumare SP Indaiatuba SP

If I understand your question correctly, you are trying to get all of the "fields" objects from the JSON into your ObservableCollection<Fields> . 如果我正确理解了您的问题,则您正在尝试将JSON中的所有“字段”对象放入您的ObservableCollection<Fields> Here is how you can do that: 这是您可以执行的操作:

JArray jObj = JArray.Parse(json);

Items = new ObservableCollection<Fields>(
        jObj.Children().Select(jo => jo["result"]["fields"].ToObject<Fields>()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM