【问题标题】:Windows Phone with Json - Error reading JObject from JsonReader带有 Json 的 Windows Phone - 从 JsonReader 读取 JObject 时出错
【发布时间】:2014-08-11 19:24:23
【问题描述】:

我正在为 Windows Phone 开发一个应用程序,其中 ListBox 显示来自 Json 文件的数据。 当我的 Json 文件有一项时,我的代码可以正常工作,但是当我的 Json 文件有一项时,异常是

"从 JsonReader 读取 JObject 时出错。当前 JsonReader 项不是 对象:StartArray。路径“,line1,位置1。”

Json1 正常工作时:

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

Json2 工作不正常时:

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

我的代码:

 public PivotPage1()
    {
        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
                JObject jObj = JObject.Parse(json);

                // Extract what you need, the "fields" property
                JToken jToken = jObj["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; }
    }
        private void AddProd(object sender, RoutedEventArgs e)
    {

        if (ListBoxx.SelectedItem != null)
        {
            Fields fi = (Fields)this.ListBoxx.SelectedItem;


            ListBoxx2.Items.Add(fi);



        }
        else
        {
            MessageBox.Show("Selecione um item para adicionar!");
        }
    }

【问题讨论】:

    标签: c# json exception windows-phone-8


    【解决方案1】:

    您应该将 json 解析为数组而不是对象。您最好的选择是使 json 保持一致,即始终有一个集合,即使您只有一个项目。

    然后你可以像这样解析你的json:

    JArray arr = JArray.Parse(json);
    
    foreach (JObject obj in arr.Children<JObject>())
    {
    ...
    }
    

    【讨论】:

    • 但是为什么第一个示例可以正常工作?我正在使用对象
    • 因为您的第一个 JSON 是对象的表示。而第二个 JSON 是一个对象数组。由于您可以拥有多个对象,因此请尝试将所有数据呈现为集合(即数组)。如果您无法控制收到的 JSON 数据,那么您可以检测是否获得了一个对象或一组对象。对于前者,您可以使用现有代码,对于后者 - 您需要将 JSON 解析为数组并遍历集合。
    • 感谢您的支持
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2022-11-27
    相关资源
    最近更新 更多