22

我有一个这样的对象数组,json格式如下:

{"results":[{"SwiftCode":"","City":"","BankName":"Deutsche Bank","Bankkey":"10020030","Bankcountry":"DE"},{"SwiftCode":"","City":"10891 Berlin","BankName":"Commerzbank Berlin (West)","Bankkey":"10040000","Bankcountry":"DE"}]}

我想得到的是object[]C# 中的一个,其中一个对象包含一个 json 对象中的所有数据。问题是,我不能这里这样使用这个对象的属性创建一个类:

public class Result
{
    public int SwiftCode { get; set; }
    public string City { get; set; }
    //      .
    //      .
    public string Bankcountry { get; set; }
}

因为我每次都会得到不同的结果,但我知道它总是一个对象数组。有人知道我如何设法取回一组对象吗?

编辑

我必须通过这个对象传递给 powershell WriteObject(results)。所以输出应该只是array.

4

6 回答 6

19

虽然这是一个老问题,但我想我还是会发布我的答案,如果这对未来的人有帮助的话

 JArray array = JArray.Parse(jsonString);
 foreach (JObject obj in array.Children<JObject>())
 {
     foreach (JProperty singleProp in obj.Properties())
     {
         string name = singleProp.Name;
         string value = singleProp.Value.ToString();
         //Do something with name and value
         //System.Windows.MessageBox.Show("name is "+name+" and value is "+value);
      }
 }

此解决方案使用 Newtonsoft 库,不要忘记包含using Newtonsoft.Json.Linq;

于 2016-05-01T17:20:42.527 回答
18

像这样使用newtonsoft

using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

class Program
{
    static void Main()
    {
        string json = "{'results':[{'SwiftCode':'','City':'','BankName':'Deutsche    Bank','Bankkey':'10020030','Bankcountry':'DE'},{'SwiftCode':'','City':'10891    Berlin','BankName':'Commerzbank Berlin (West)','Bankkey':'10040000','Bankcountry':'DE'}]}";

        var resultObjects = AllChildren(JObject.Parse(json))
            .First(c => c.Type == JTokenType.Array && c.Path.Contains("results"))
            .Children<JObject>();

        foreach (JObject result in resultObjects) {
            foreach (JProperty property in result.Properties()) {
                // do something with the property belonging to result
            }
        }
    }

    // recursively yield all children of json
    private static IEnumerable<JToken> AllChildren(JToken json)
    {
        foreach (var c in json.Children()) {
            yield return c;
            foreach (var cc in AllChildren(c)) {
                yield return cc;
            }
        }
    }
}
于 2013-11-11T16:14:07.170 回答
17

使用NewtonSoft JSON.Net图书馆。

dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);

希望这可以帮助。

于 2013-11-11T15:57:59.100 回答
11

我刚刚得到了一个更简单的解决方案,从 JSON 对象中获取列表。希望这能有所帮助。

我得到了这样的 JSON:

{"Accounts":"[{\"bank\":\"Itau\",\"account\":\"456\",\"agency\":\"0444\",\"digit\":\"5\"}]"}

并制作了一些这样的类型

    public class FinancialData
{
    public string Accounts { get; set; } // this will store the JSON string
    public List<Accounts> AccountsList { get; set; } // this will be the actually list. 
}

    public class Accounts
{
    public string bank { get; set; }
    public string account { get; set; }
    public string agency { get; set; }
    public string digit { get; set; }

}

和“魔法”部分

 Models.FinancialData financialData = (Models.FinancialData)JsonConvert.DeserializeObject(myJSON,typeof(Models.FinancialData));
        var accounts = JsonConvert.DeserializeObject(financialData.Accounts) as JArray;

        foreach (var account in  accounts)
        {
            if (financialData.AccountsList == null)
            {
                financialData.AccountsList = new List<Models.Accounts>();
            }

            financialData.AccountsList.Add(JsonConvert.DeserializeObject<Models.Accounts>(account.ToString()));
        }
于 2016-11-01T11:25:55.927 回答
1

我相信这要简单得多;

dynamic obj = JObject.Parse(jsonString);
string results  = obj.results;
foreach(string result in result.Split('))
{
//Todo
}
于 2015-05-14T20:12:41.437 回答
1

使用.NET 6你可以使用内置System.Text.Json.Nodes

例子:

string json = @"{""results"":[{""SwiftCode"":"""",""City"":"""",""BankName"":""Deutsche Bank"",""Bankkey"":""10020030"",""Bankcountry"":""DE""},{""SwiftCode"":"""",""City"":""10891 Berlin"",""BankName"":""Commerzbank Berlin (West)"",""Bankkey"":""10040000"",""Bankcountry"":""DE""}]}";

JsonObject obj = JsonNode.Parse(json).AsObject();
JsonArray jsonArray = (JsonArray)obj["results"];

在此处输入图像描述

于 2021-11-10T15:47:04.413 回答