C# 解析 JSON 对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19910476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Parsing JSON array of objects
提问by AstralisSomnium
I have an array of objects like this in json
format:
我有一个这样的对象数组,json
格式如下:
{"results":[{"SwiftCode":"","City":"","BankName":"Deutsche Bank","Bankkey":"10020030","Bankcountry":"DE"},{"SwiftCode":"","City":"10891 Berlin","BankName":"Commerzbank Berlin (West)","Bankkey":"10040000","Bankcountry":"DE"}]}
What I want to get is a object[]
in C#, where one object contains all the data what is in one json object. The thing is, I can NOTmake a class with the properties of this object like here:
我想得到的是object[]
C# 中的一个,其中一个对象包含一个 json 对象中的所有数据。问题是,我可以不使一个类与此对象喜欢这里的属性:
public class Result
{
public int SwiftCode { get; set; }
public string City { get; set; }
// .
// .
public string Bankcountry { get; set; }
}
Because I get everytime different results back, but I know it's always an array of objects. Someone knows how I could manage to get an array of objects back?
因为我每次都会得到不同的结果,但我知道它总是一个对象数组。有人知道我如何设法取回一组对象吗?
EDIT
编辑
I have to pass this object to powershell via WriteObject(results)
. So the ouput should only be the object IN the array
.
我必须通过WriteObject(results)
. 所以输出应该只是array
.
采纳答案by allonhadaya
Use newtonsoftlike so:
像这样使用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;
}
}
}
}
回答by Marvin Smit
Use NewtonSoft JSON.Net
library.
使用NewtonSoft JSON.Net
图书馆。
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
Hope this helps.
希望这可以帮助。
回答by mca
I believe this is much simpler;
我相信这要简单得多;
dynamic obj = JObject.Parse(jsonString);
string results = obj.results;
foreach(string result in result.Split('))
{
//Todo
}
回答by Bibaswann Bandyopadhyay
Though this is an old question, I thought I'd post my answer anyway, if that helps someone in future
虽然这是一个老问题,但我想我还是会发布我的答案,如果这对将来有帮助的话
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);
}
}
This solution uses Newtonsoft library, don't forget to include using Newtonsoft.Json.Linq;
此解决方案使用 Newtonsoft 库,不要忘记包含 using Newtonsoft.Json.Linq;
回答by Luiz Paulo
I have just got an solution a little bit easier do get an list out of an JSON object. Hope this can help.
我刚刚得到了一个更简单的解决方案,可以从 JSON 对象中获取列表。希望这能有所帮助。
I got an JSON like this:
我得到了这样的 JSON:
{"Accounts":"[{\"bank\":\"Itau\",\"account\":\"456\",\"agency\":\"0444\",\"digit\":\"5\"}]"}
And made some types like this
并制作了一些这样的类型
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; }
}
and the "magic" part
和“魔法”部分
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()));
}
回答by Remil k.r
string jsonData1=@"[{""name"":""0"",""price"":""40"",""count"":""1"",""productId"":""4"",""catid"":""4"",""productTotal"":""40"",""orderstatus"":""0"",""orderkey"":""123456789""}]";
string jsonData1=@"[{""name"":""0"",""price"":""40"",""count"":""1"",""productId"":" "4"",""catid"":""4"",""productTotal"":""40"",""orderstatus"":""0"",""orderkey""":""123456789 ""}]";
string jsonData = jsonData1.Replace("\"", "");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
JArray array= JArray.Parse(jsonData);
couldnot parse , if the vaule is a string..
无法解析,如果 vaule 是一个字符串..
look at name : meals , if name : 1 then it will parse
查看名称:膳食,如果名称:1,则它会解析