解析JSON表示对象引用未设置为对象xamarin.forms的实例

时间:2017-08-10 10:11:17

标签: xamarin xamarin.forms json.net

我正在尝试解析JSON但是它给了我错误,因为对象引用没有设置为对象的实例 我在想的是 JSON:

    [
    {"Product":"Product 1",
    "Origins":
    [
        {"Origin":"XXX QQ",
        "Details":   
        [
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}
        ]
        },
        {"Origin":"ADAMI Oil",
        "Details":   
        [
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}
        ]
        }       
    ]
    },
    {"Product":"Product 2",
    "Origins":
    [
        {"Origin":"YYY pp",
        "Details":   
        [
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}
        ]
        },
        {"Origin":"PQR CCC",
        "Details":   
        [
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},
            {"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}
        ]
        }       
    ]
    }
]

包含JSON值的我的对象类:

public class Products
{
    public List<Origins> ListOrigin { get; set; }
    public string PRODUCT { get; set; }
}
public class Origins
{
    public string Origin { get; set; }
    public List<Details> ListDetails { get; set; }
}
public class Details
{
    public string Seller { get; set; }
    public string Seller_Rate { get; set; }
    public string Saller_QTY { get; set; }
    public string Buyer { get; set; }
    public string Buyer_Rate { get; set; }
    public string Buyer_QTY { get; set; }
}

调用Web服务并解析:

string responsejson = await client.GetStringAsync(Constants.url_getproducts);
        res = JsonConvert.DeserializeObject<List<Products>>(responsejson);
        try
        {
            foreach (Products p in res)
            {
                Log.Debug("Login1", p.PRODUCT);

                foreach (Origins o in p.ListOrigin)
                {
                    Log.Debug("Login1", o.Origin);
                }
            }
        }
        catch (Exception e){
            Log.Debug("Login e", e.Message);
            Log.Debug("Login e", e.StackTrace);
        }

这时当我试图用 foreach 解析它时,Log.Debug(&#34; Login1&#34;,p.PRODUCT);这给了产品正确,但对于Origins,它说

对象引用未设置为对象的实例。

我得到的一个解决方案是:

try
        {
            root = JsonConvert.DeserializeObject<List<Products>>(responsejson);
            Log.Debug("Login",Convert.ToString(root.Count));
            for(int i=0;i<root.Count;i++)
            {
                Log.Debug("Login", "=============================");
                Log.Debug("Login", root[i].PRODUCT);
                for(int j=0;j<root[i].Origins.Count;j++)
                {
                    Log.Debug("Login","   "+root[i].Origins[j].Origin);
                    for(int k=0;k<root[i].Origins[j].Details.Count;k++)
                    {
                        Log.Debug("Login", "       Buy " + root[i].Origins[j].Details[k].Buyer+" BUY_QTY "+ root[i].Origins[j].Details[k].Buyer_QTY);

                    }
                }
                Log.Debug("Login", "=============================");
            }
        }

0 个答案:

没有答案