问题描述
我正在为项目提取大量JSON数据.每个项目都有一组我想获取的特定数据,并将其放置在列表中.
I am pulling in a large amount of JSON data for items. Each item has set of particular data that I am wanting to grab and place in a list.
这里是JSON数据中700多个项目中的两个的样本.我需要能够使用Newtonsoft.JSON遍历JSON数据,并在"fieldName":"Item ID"的"items","values","value"处提取数据.我需要对JSON数据中返回的所有700个项目执行此操作.如何遍历此数据并仅访问特定数据?
Here is a sample of two of the over 700 items contained in the JSON data.I need to be able to iterate through the JSON data using Newtonsoft.JSON and pull the data at "items", "values", "value" for the "fieldName": "Item ID". I need to do this for all 700 items returned in the JSON data. How do I iterate through this data and access just that particular data?
{
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);
推荐答案
以下内容很骇人,但有效:
The following is hacky but works:
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);
}
}
}
}
}
这篇关于使用C#:如何使用Newtonsoft.JSON遍历嵌套的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!