如何反序列化JSON字符串



我有一个序列化的JSON字符串

string json = " [
                {
                    "id": 1,
                    "barcode": "TestBARCODE",
                    "nsr": 0,
                    "stk_in": 0,
                    "stk_out": 0,
                    "sales": 0,
                    "balance": 1
                },
                {
                    "id": 2,
                    "barcode": "TestBARCODE2",
                    "nsr": 0,
                    "stk_in": 0,
                    "stk_out": 0,
                    "sales": 0,
                    "balance": 1
                },
                {
                    "id": 3,
                    "barcode": "TestBARCODE3",
                    "nsr": 0,
                    "stk_in": 0,
                    "stk_out": 0,
                    "sales": 0,
                    "balance": 1
                },
                {
                    "id": 4,
                    "barcode": "AAA",
                    "nsr": 0,
                    "stk_in": 0,
                    "stk_out": 0,
                    "sales": 0,
                    "balance": 1
                },
                {
                    "id": 5,
                    "barcode": "BBB",
                    "nsr": 0,
                    "stk_in": 0,
                    "stk_out": 0,
                    "sales": 0,
                    "balance": 1
                }
            ]"

我需要迭代每一行获取id,条形码,nsr等及其值。我正在使用Newtonsoft,但不能使用他们的示例Movie m = JsonConvert.DeserializeObject<Movie>(json);

我试过var m = JsonConvert.DeserializeObject<JValue>(json);,它确实得到返回JSON行。由于某些原因,m.Value是一个字符串类型。有什么好主意吗?

由于在示例中定义的json字符串表示集合,因此必须反序列化到List。我创建了这个。net小提琴来演示您的场景。您可以在现场检查输出。

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
    public static void Main()
    {
        string json = "[{"id":1,"barcode":"TestBARCODE","nsr":0,"stk_in":0,"stk_out":0,"sales":0,"balance":1},{"id":2,"barcode":"TestBARCODE2","nsr":0,"stk_in":0,"stk_out":0,"sales":0,"balance":1},{"id":3,"barcode":"TestBARCODE3","nsr":0,"stk_in":0,"stk_out":0,"sales":0,"balance":1},{"id":4,"barcode":"AAA","nsr":0,"stk_in":0,"stk_out":0,"sales":0,"balance":1},{"id":5,"barcode":"BBB","nsr":0,"stk_in":0,"stk_out":0,"sales":0,"balance":1}]";
        var movies = JsonConvert.DeserializeObject<List<Movie>>(json);
        foreach (var movie in movies)
        {
            Console.WriteLine("Movie Id : " + movie.Id + " BarCode : " + movie.Barcode);
        }
    }
}
public class Movie
{
    public int Id {get; set; }
    public string Barcode {get; set; }
    public int NSR {get; set; }

    public int Stk_in {get; set; }
    public int Stk_out {get; set; }
    public int Balance {get; set; }
}

除了@StriplingWarriors解决方案:我猜你的Movie声明不适合json字符串。像这样定义你的类:

// tip: use a tool like http://json2csharp.com/ to avoid typos etc.
public class MyObject
{
    public int id {get;set;}
    public string barcode {get;set;}
    public int nsr {get;set;}
    public int stk_in {get;set;}
    public int stk_out {get;set;}
    public int sales {get;set;}
    public int balance {get;set;}
}

然后将json反序列化为类的List,如下所示:

var m = JsonConvert.DeserializeObject<List<MyObject>>(json);

JValue s只能表示像字符串和数字这样的基本值,所以当您将字符串反序列化为JValue时,它将成为string支持的值是有意义的。

试试这个:

var m = JsonConvert.DeserializeObject<JArray>(json);

:

foreach(var item in m)
{
    Console.WriteLine(item["id"]);
    ...
}

试试这个:

public class Data
{
    public int id { get; set; }
    public string barcode { get; set; }
    public int nsr { get; set; }
    public int stk_in { get; set; }
    public int stk_out { get; set; }
    public int sales { get; set; }
    public int balance { get; set; }
}
public class RootObject
{
    public List<Data> data { get; set; }
}   
RootObject objRootObject = JsonConvert.DeserializeObject<RootObject>(json);
if (objRootObject.data.Count > 0)
{
    foreach (var item in objRootObject.data)
    {
        Console.WriteLine(item.id);
        Console.WriteLine(item.barcode);
        Console.WriteLine(item.nsr);
    }
}

这对你确实很有帮助。

相关内容

  • 没有找到相关文章

最新更新