我正在为项目提供大量的JSON数据。每个项目都有一组我想要抓取并放在列表中的特定数据。
以下是JSON数据中包含的700多个项目中的两个的示例。 我需要能够使用Newtonsoft.JSON迭代JSON数据并将数据拉到" items"," values"," value"对于" fieldName":" Item ID"。我需要为JSON数据中返回的所有700个项目执行此操作。如何迭代这些数据并仅访问该特定数据?
{
items: [
{
"createDate": 1468872878834,
"updateDate": 1498075185297,
"deleted": 0,
"allListsItemCount": 0,
"assets": [
{
"createDate": 1469129652323,
"updateDate": 1471625094427,
"deleted": 0,
"url": "A_O_Smith_SQ1032_lg.jpg",
"parentId": 10270308,
"typeId": 4000014573,
"filename": "A_O_Smith_SQ1032_lg.jpg",
"seqOrder": 1,
"printActive": 1,
"webActive": 1,
"thumbnail": 1,
"flag1": 1,
"typeName": "Web Thumbnail Image",
"typeGroupCode": "PRIM",
"id": 4063212155,
"transformationSource": false,
"primaryAsset": false
},
{
"createDate": 1469129652323,
"updateDate": 1471625094427,
"deleted": 0,
"url": "A_O_Smith_SQ1032.tif",
"parentId": 10270308,
"typeId": 4000014572,
"filename": "A_O_Smith_SQ1032.tif",
"seqOrder": 2,
"printActive": 1,
"webActive": 0,
"typeName": "Print Image",
"typeGroupCode": "PRIM",
"id": 4063212156,
"transformationSource": false,
"primaryAsset": false
}
],
"masterCategoryId": 4000593879,
"listsCount": 0,
"advTablesCount": 0,
"id": 10270308,
"values": [
{
"languageId": 1,
"fieldId": 20002078,
"fieldName": "Amps",
"value": "19.20/9.60"
},
{
"languageId": 1,
"fieldId": 20000921,
"fieldKey": "Item_ID",
"fieldName": "Item ID",
"value": "USQ1152"
},
{
"languageId": 1,
"fieldId": 20002075,
"fieldKey": "Inv_Mast_UID",
"fieldName": "Inv_Mast_UID",
"value": "6736"
},
{
"languageId": 1,
"fieldId": 20002081,
"fieldName": "Bearings",
"value": "Sealed Ball"
},
{
"languageId": 1,
"fieldId": 20002141,
"fieldKey": "Web_Description",
"fieldName": "Web&UPC Description",
"value": "CENTURY MOTOR NS"
},
{
"languageId": 1,
"fieldId": 20002190,
"fieldKey": "Web_Image",
"fieldName": "Image Name LR",
"value": "A_O_Smith_SQ1032_lg"
},
{
"languageId": 1,
"fieldId": 20002191,
"fieldName": "Image Name HR",
"value": "A_O_Smith_SQ1032"
},
{
"languageId": 1,
"fieldId": 20002089,
"fieldName": "Features",
"value": "Used on many Sta-Rite - Red Jacket, Pac Fab & American pprojets and Sears jet and pool pumps,High & low service factors, 303 Stainless steel threaded shaft, 50 Degrees C ambient, Capacitor sart"
},
{
"languageId": 1,
"fieldId": 20002088,
"fieldName": "Enclosure",
"value": "Open Drip Proof"
},
{
"languageId": 1,
"fieldId": 20002091,
"fieldName": "Frame"
},
{
"languageId": 1,
"fieldId": 20002092,
"fieldName": "H.P.",
"value": "1-1/2"
},
{
"languageId": 1,
"fieldId": 20002093,
"fieldName": "Hertz",
"value": "60"
},
{
"languageId": 1,
"fieldId": 20002524,
"fieldName": "Mounting Option",
"value": "Square Flange"
},
{
"languageId": 1,
"fieldId": 20002104,
"fieldName": "Overload",
"value": "Automatic"
},
{
"languageId": 1,
"fieldId": 20002105,
"fieldName": "Phase",
"value": "Single"
},
{
"languageId": 1,
"fieldId": 20002109,
"fieldName": "RPM",
"value": "3450"
},
{
"languageId": 1,
"fieldId": 20002110,
"fieldName": "Service Factor",
"value": "1.1"
},
{
"languageId": 1,
"fieldId": 20002112,
"fieldName": "Speeds",
"value": "Single"
},
{
"languageId": 1,
"fieldId": 20002114,
"fieldName": "Voltage",
"value": "115/230"
},
{
"languageId": 1,
"fieldId": 20002108,
"fieldName": "Rotation",
"value": "CCW Pump End"
}
],
"number": "USQ1152",
"type": "item",
"statusId": 4000004050
},
{
"createDate": 1468872878845,
"updateDate": 1495111010291,
"deleted": 0,
"allListsItemCount": 0,
"assets": [
{
"createDate": 1469129652335,
"updateDate": 1471625094427,
"deleted": 0,
"url": "A_O_Smith_SQ1032_lg.jpg",
"parentId": 10270309,
"typeId": 4000014573,
"filename": "A_O_Smith_SQ1032_lg.jpg",
"seqOrder": 1,
"printActive": 1,
"webActive": 1,
"thumbnail": 1,
"flag1": 1,
"typeName": "Web Thumbnail Image",
"typeGroupCode": "PRIM",
"id": 4063212157,
"transformationSource": false,
"primaryAsset": false
},
{
"createDate": 1469129652335,
"updateDate": 1471625094427,
"deleted": 0,
"url": "A_O_Smith_SQ1032.tif",
"parentId": 10270309,
"typeId": 4000014572,
"filename": "A_O_Smith_SQ1032.tif",
"seqOrder": 2,
"printActive": 1,
"webActive": 0,
"typeName": "Print Image",
"typeGroupCode": "PRIM",
"id": 4063212158,
"transformationSource": false,
"primaryAsset": false
}
],
"masterCategoryId": 4000593879,
"listsCount": 0,
"advTablesCount": 0,
"id": 10270309,
"values": [
{
"languageId": 1,
"fieldId": 20002078,
"fieldName": "Amps",
"value": "10.4"
},
{
"languageId": 1,
"fieldId": 20000921,
"fieldKey": "Item_ID",
"fieldName": "Item ID",
"value": "USQ1202"
},
{
"languageId": 1,
"fieldId": 20002075,
"fieldKey": "Inv_Mast_UID",
"fieldName": "Inv_Mast_UID",
"value": "6737"
},
{
"languageId": 1,
"fieldId": 20002141,
"fieldKey": "Web_Description",
"fieldName": "Web&UPC Description",
"value": "CENTURY MOTOR NS"
},
{
"languageId": 1,
"fieldId": 20002190,
"fieldKey": "Web_Image",
"fieldName": "Image Name LR",
"value": "A_O_Smith_SQ1032_lg"
},
{
"languageId": 1,
"fieldId": 20002191,
"fieldName": "Image Name HR",
"value": "A_O_Smith_SQ1032"
},
{
"languageId": 1,
"fieldId": 20002089,
"fieldName": "Features",
"value": "Used on many Sta-Rite - Red Jacket, Pac Fab & American pprojets and Sears jet and pool pumps,High & low service factors, 303 Stainless steel threaded shaft, 50 Degrees C ambient, Capacitor sart"
},
{
"languageId": 1,
"fieldId": 20002102,
"fieldName": "Notes",
"value": "199"
},
{
"languageId": 1,
"fieldId": 20002088,
"fieldName": "Enclosure",
"value": "Open Drip Proof"
},
{
"languageId": 1,
"fieldId": 20002091,
"fieldName": "Frame"
},
{
"languageId": 1,
"fieldId": 20002092,
"fieldName": "H.P.",
"value": "2"
},
{
"languageId": 1,
"fieldId": 20002093,
"fieldName": "Hertz",
"value": "60"
},
{
"languageId": 1,
"fieldId": 20002524,
"fieldName": "Mounting Option",
"value": "NEMA 56C Face"
},
{
"languageId": 1,
"fieldId": 20002104,
"fieldName": "Overload",
"value": "Automatic"
},
{
"languageId": 1,
"fieldId": 20002105,
"fieldName": "Phase",
"value": "Single"
},
{
"languageId": 1,
"fieldId": 20002109,
"fieldName": "RPM",
"value": "3450"
},
{
"languageId": 1,
"fieldId": 20002110,
"fieldName": "Service Factor",
"value": "1.1"
},
{
"languageId": 1,
"fieldId": 20002112,
"fieldName": "Speeds",
"value": "Single"
},
{
"languageId": 1,
"fieldId": 20002114,
"fieldName": "Voltage",
"value": "230"
},
{
"languageId": 1,
"fieldId": 20002108,
"fieldName": "Rotation",
"value": "CCWPE-Not Reversible"
}
],
"number": "USQ1202",
"type": "item",
"statusId": 4000004050
}
]
}
这就是我的开始:
string itemJson = await response.Content.ReadAsStringAsync();
JObject rss = JObject.Parse(itemJson);
答案 0 :(得分:1)
这样的东西?
foreach (var item in items)
{
Console.WriteLine("{0}", item.id);
foreach (var val in item.values)
{
Console.WriteLine("{0}", val.fieldName);
}
}
答案 1 :(得分:1)
一种方法是使用http://json2csharp.com/从json生成类。
获得类后,可以将它们添加到项目中并使用JSON.net反序列化整个结构。一旦你将json反序列化为c#POCO之类的
var yourObject = JsonConvert.DeserializeObject<'your c# wrapper class'>(<your json>);
您可以使用Linq查询对象。
答案 2 :(得分:1)
以下是hacky但有效:
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
public static void Main()
{
string itemJson = @"{'items':[{'createDate':1468872878834,'updateDate':1498075185297,'deleted':0,'allListsItemCount':0,'assets':[{'createDate':1469129652323,'updateDate':1471625094427,'deleted':0,'url':'A_O_Smith_SQ1032_lg.jpg','parentId':10270308,'typeId':4000014573,'filename':'A_O_Smith_SQ1032_lg.jpg','seqOrder':1,'printActive':1,'webActive':1,'thumbnail':1,'flag1':1,'typeName':'Web Thumbnail Image','typeGroupCode':'PRIM','id':4063212155,'transformationSource':false,'primaryAsset':false},{'createDate':1469129652323,'updateDate':1471625094427,'deleted':0,'url':'A_O_Smith_SQ1032.tif','parentId':10270308,'typeId':4000014572,'filename':'A_O_Smith_SQ1032.tif','seqOrder':2,'printActive':1,'webActive':0,'typeName':'Print Image','typeGroupCode':'PRIM','id':4063212156,'transformationSource':false,'primaryAsset':false}],'masterCategoryId':4000593879,'listsCount':0,'advTablesCount':0,'id':10270308,'values':[{'languageId':1,'fieldId':20002078,'fieldName':'Amps','value':'19.20/9.60'},{'languageId':1,'fieldId':20000921,'fieldKey':'Item_ID','fieldName':'Item ID','value':'USQ1152'},{'languageId':1,'fieldId':20002075,'fieldKey':'Inv_Mast_UID','fieldName':'Inv_Mast_UID','value':'6736'},{'languageId':1,'fieldId':20002081,'fieldName':'Bearings','value':'Sealed Ball'},{'languageId':1,'fieldId':20002141,'fieldKey':'Web_Description','fieldName':'Web&UPC Description','value':'CENTURY MOTOR NS'},{'languageId':1,'fieldId':20002190,'fieldKey':'Web_Image','fieldName':'Image Name LR','value':'A_O_Smith_SQ1032_lg'},{'languageId':1,'fieldId':20002191,'fieldName':'Image Name HR','value':'A_O_Smith_SQ1032'},{'languageId':1,'fieldId':20002089,'fieldName':'Features','value':'Used on many Sta-Rite - Red Jacket, Pac Fab & American pprojets and Sears jet and pool pumps,High & low service factors, 303 Stainless steel threaded shaft, 50 Degrees C ambient, Capacitor sart'},{'languageId':1,'fieldId':20002088,'fieldName':'Enclosure','value':'Open Drip Proof'},{'languageId':1,'fieldId':20002091,'fieldName':'Frame'},{'languageId':1,'fieldId':20002092,'fieldName':'H.P.','value':'1-1/2'},{'languageId':1,'fieldId':20002093,'fieldName':'Hertz','value':'60'},{'languageId':1,'fieldId':20002524,'fieldName':'Mounting Option','value':'Square Flange'},{'languageId':1,'fieldId':20002104,'fieldName':'Overload','value':'Automatic'},{'languageId':1,'fieldId':20002105,'fieldName':'Phase','value':'Single'},{'languageId':1,'fieldId':20002109,'fieldName':'RPM','value':'3450'},{'languageId':1,'fieldId':20002110,'fieldName':'Service Factor','value':'1.1'},{'languageId':1,'fieldId':20002112,'fieldName':'Speeds','value':'Single'},{'languageId':1,'fieldId':20002114,'fieldName':'Voltage','value':'115/230'},{'languageId':1,'fieldId':20002108,'fieldName':'Rotation','value':'CCW Pump End'}],'number':'USQ1152','type':'item','statusId':4000004050},{'createDate':1468872878845,'updateDate':1495111010291,'deleted':0,'allListsItemCount':0,'assets':[{'createDate':1469129652335,'updateDate':1471625094427,'deleted':0,'url':'A_O_Smith_SQ1032_lg.jpg','parentId':10270309,'typeId':4000014573,'filename':'A_O_Smith_SQ1032_lg.jpg','seqOrder':1,'printActive':1,'webActive':1,'thumbnail':1,'flag1':1,'typeName':'Web Thumbnail Image','typeGroupCode':'PRIM','id':4063212157,'transformationSource':false,'primaryAsset':false},{'createDate':1469129652335,'updateDate':1471625094427,'deleted':0,'url':'A_O_Smith_SQ1032.tif','parentId':10270309,'typeId':4000014572,'filename':'A_O_Smith_SQ1032.tif','seqOrder':2,'printActive':1,'webActive':0,'typeName':'Print Image','typeGroupCode':'PRIM','id':4063212158,'transformationSource':false,'primaryAsset':false}],'masterCategoryId':4000593879,'listsCount':0,'advTablesCount':0,'id':10270309,'values':[{'languageId':1,'fieldId':20002078,'fieldName':'Amps','value':'10.4'},{'languageId':1,'fieldId':20000921,'fieldKey':'Item_ID','fieldName':'Item ID','value':'USQ1202'},{'languageId':1,'fieldId':20002075,'fieldKey':'Inv_Mast_UID','fieldName':'Inv_Mast_UID','value':'6737'},{'languageId':1,'fieldId':20002141,'fieldKey':'Web_Description','fieldName':'Web&UPC Description','value':'CENTURY MOTOR NS'},{'languageId':1,'fieldId':20002190,'fieldKey':'Web_Image','fieldName':'Image Name LR','value':'A_O_Smith_SQ1032_lg'},{'languageId':1,'fieldId':20002191,'fieldName':'Image Name HR','value':'A_O_Smith_SQ1032'},{'languageId':1,'fieldId':20002089,'fieldName':'Features','value':'Used on many Sta-Rite - Red Jacket, Pac Fab & American pprojets and Sears jet and pool pumps,High & low service factors, 303 Stainless steel threaded shaft, 50 Degrees C ambient, Capacitor sart'},{'languageId':1,'fieldId':20002102,'fieldName':'Notes','value':'199'},{'languageId':1,'fieldId':20002088,'fieldName':'Enclosure','value':'Open Drip Proof'},{'languageId':1,'fieldId':20002091,'fieldName':'Frame'},{'languageId':1,'fieldId':20002092,'fieldName':'H.P.','value':'2'},{'languageId':1,'fieldId':20002093,'fieldName':'Hertz','value':'60'},{'languageId':1,'fieldId':20002524,'fieldName':'Mounting Option','value':'NEMA 56C Face'},{'languageId':1,'fieldId':20002104,'fieldName':'Overload','value':'Automatic'},{'languageId':1,'fieldId':20002105,'fieldName':'Phase','value':'Single'},{'languageId':1,'fieldId':20002109,'fieldName':'RPM','value':'3450'},{'languageId':1,'fieldId':20002110,'fieldName':'Service Factor','value':'1.1'},{'languageId':1,'fieldId':20002112,'fieldName':'Speeds','value':'Single'},{'languageId':1,'fieldId':20002114,'fieldName':'Voltage','value':'230'},{'languageId':1,'fieldId':20002108,'fieldName':'Rotation','value':'CCWPE-Not Reversible'}],'number':'USQ1202','type':'item','statusId':4000004050}]}";
dynamic rss = JObject.Parse(itemJson);
dynamic items = rss.items;
foreach (dynamic item in items)
{
dynamic values = item.values;
foreach (dynamic val in values)
{
//Console.WriteLine(val);
if (val.fieldName == "Item ID")
{
Console.WriteLine(val.value);
}
}
}
}
}