C# 带Json的Windows Phone-从JsonReader读取JObject时出错

C# 带Json的Windows Phone-从JsonReader读取JObject时出错,c#,json,exception,windows-phone-8,C#,Json,Exception,Windows Phone 8,我正在为Windows Phone开发一个应用程序,其中一个列表框显示Json文件中的数据。 当我的Json文件有一个项目时,我的代码可以正常工作,但当有多个项目时,则会出现异常 “从JsonReader读取JObject时出错。当前JsonReader项不可用。” 对象:StartArray.Path“,第1行,位置1。” Json1工作正常时: {"xId":"52","result":{"type":"Basico.Bean.MunicipioClass.TMunicipio","id":

我正在为Windows Phone开发一个应用程序,其中一个列表框显示Json文件中的数据。 当我的Json文件有一个项目时,我的代码可以正常工作,但当有多个项目时,则会出现异常

“从JsonReader读取JObject时出错。当前JsonReader项不可用。” 对象:StartArray.Path“,第1行,位置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!");
        }
    }
公共数据透视页面1()
{
初始化组件()
字符串文本;
使用(var store=IsolatedStorageFile.GetUserStoreForApplication())
使用(var readStream=new-IsolatedStorageFileStream(“json.html”,FileMode.Open,FileAccess.Read,FileShare.Read,store))
使用(var reader=newstreamreader(readStream))
{
text=reader.ReadToEnd();
}
{
尝试
{
DataContext=this;
//您的JSON字符串
字符串json=文本;
//解析为JObject
JObject-jObj=JObject.Parse(json);
//提取您需要的“字段”属性
JToken JToken=jObj[“结果”][“字段”];
//转换为字段类实例
Fields=jToken.ToObject();
Items=newobserveCollection(){fields};
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}
公共ObservableCollection项{get;set;}
公共类字段
{
[JsonProperty(PropertyName=“FId”)]
公共int FId{get;set;}
公共字符串FNome{get;set;}
公共字符串FEstado{get;set;}
公共字符串FPais{get;set;}
}
私有void AddProd(对象发送方,路由目标)
{
if(ListBoxx.SelectedItem!=null)
{
字段fi=(字段)this.ListBoxx.SelectedItem;
ListBoxx2.Items.Add(fi);
}
其他的
{
MessageBox.Show(“Selecione um item para adicionar!”);
}
}

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

然后您可以解析json,如下所示:

JArray arr = JArray.Parse(json);

foreach (JObject obj in arr.Children<JObject>())
{
...
}
JArray arr=JArray.Parse(json); foreach(arr.Children()中的JObject对象) { ... }
但是为什么第一个示例工作得很好呢?我使用object是因为您的第一个JSON是对象的表示。而第二个JSON是对象的数组。由于您可以有多个对象,所以请尝试将所有数据显示为集合(即数组)。如果您无法控制接收的JSON数据,则可以检测您是否获得一个对象或对象数组。对于前者,您可以使用现有代码;对于后者,您需要将JSON解析为数组并迭代集合。感谢您的支持