问题:从JSON文件中提取数据并将其存储在java中以进行调用的方法。
我正在尝试做什么:我想从JSON文件中提取数据,以使用Java生成屏幕上数据的比较。
我尝试过:首先,我试图找到有关从JSON文件中提取数据的资源和信息,但后来尝试通过手动操作文本来实现,但这显然不可扩展。
守则
{
"searchRoutes":
{
"0": "refinements"
}
"searchResults":
{
"classifiedAdverts":
{
"0":
{
"id": "201401311453322"
"capId": "20524"
"channel": "cars"
"distanceFromVehicleLocation": 0
"searchTarget": "usedcars"
"advertAttributes":
{
"advertTitle": "Ford Fiesta 1.3 Flight 3dr +PX TO CLEAR+NO MOT-TAX+"
"description": "RED, 07989 715714, Tilt/removable glass sunroof, 14" 7 spoke alloy wheels, Central locking, Stereo radio/cassette model 3000NT/2 speakers, Immobiliser-Passive Anti-Theft System (PATS), Driver's seat manual height adjustment. £195 p/x welcome"
"advertVersionNumber": 7
"advertPlacementDate": 20140131152126
"advertPlacementDateFormatted": "31/01/2014 15:21:26"
"daysOld": 6
"isBasicAdvert": "false"
"displayAdvertiserAddress": "true"
"vehicleLatLong":
{
"0": 52.501497
"1": -1.832014
}
"town":
{
"county":
{
"region":
{
"numberOfAdverts": 0
"name": "WEST MIDLANDS"
}
"numberOfAdverts": 0
"name": "WEST MIDLANDS"
}
"numberOfAdverts": 0
"name": "BIRMINGHAM"
}
"isTrade": "true"
"isNearlyNew": "false"
"isUsedApproved": "false"
"partExchangeAllowed": "false"
"price": 195
"vehiclePriceExtra": "p/x welcome"
"telesafe": "false"
"badAdType": "OK"
"oneSearchAds": "used"
"displaySlideShow": "false"
"videoAudioDisabled": "true"
"imageInfo":
{
"imageDimensions":
{
"width": "1024"
"height": "768"
}
"advertImages":
{
"0":
{
"imageId": "3fb3f6858942f51ace201b490b953180"
"imageUrlTemplate": "http://pictures2.autotrader.co.uk/imgser-uk/imgser-uk/servlet/media?id=3fb3f6858942f51ace201b490b953180&width={width}&height={height}"
}
"1":
{
"imageId": "5cf02458c4142e9049166548015b9714"
"imageUrlTemplate": "http://pictures2.autotrader.co.uk/imgser-uk/imgser-uk/servlet/media?id=5cf02458c4142e9049166548015b9714&width={width}&height={height}"
}
}
}
"owners": 4
}
"advertiserAttributes":
{
"name": "Parkway West Midland"
"phoneNumber1":
{
"formattedPhoneNumber": "(0121) 3278777"
"shouldAddAsteriskIfIsCallTrackerNumber": "false"
}
"phoneNumber2":
{
"formattedPhoneNumber": "(0121) 3286744"
"shouldAddAsteriskIfIsCallTrackerNumber": "false"
}
"alternativePhoneNumber":
{
"formattedPhoneNumber": "(0121) 3278777"
"shouldAddAsteriskIfIsCallTrackerNumber": "false"
}
"email": "sales@parkwaybirmingham.co.uk"
"emailLink": "/redirect-email/advert/201401311453322/postcode/b82sl"
"address1": "186b Drews Lane"
"postcode": "B8 2SL"
"town": "Birmingham"
"county": "West Midlands"
}
"vehicleAttributes":
{
"make": "FORD"
"model": "FIESTA"
"yearOfManufacture": "2001"
"regLetter": "51"
"transmission": "Manual"
"fuelType": "Petrol"
"bodyType": "Hatchback"
"mileage": "105000"
"engineSize": "1300"
"derivative": "1.3 FLIGHT 3DR"
"colour": "Red"
"numDoors": "3 doors"
"vrm": "DV51LTZ"
"generationId": 11015
"vehicleRegistrationDate": "2001-10-29"
"quickQuoteInsuranceGroup": 6
"vehicleMileageUnit": "M"
"totalUserReviews": 254
"userReviewRating": 414
"vehicleCheck": "false"
"co2Emissions": 171
}
"dealerAttributes":
{
"id": "490131"
"name": "Parkway West Midland"
"email": "sales@parkwaybirmingham.co.uk"
"address1": "186b Drews Lane"
"postcode": "B8 2SL"
"town": "Birmingham"
"logoUrl": "http://dealerlogo.autotrader.co.uk/at2/adbranding/490131/images/searchlogo.gif"
"websiteLink": "http://dmsgateway.autotrader.co.uk/api/advertiser/490131/redirect/advert/source/autotrader-desktop/autotrader-advert/201401311453322?website-link=http%3A%2F%2Fwww.parkwaybirmingham.co.uk"
"oldDealerStockFlag": "N"
"stockCount": 95
"products":
{
"0": "videoSlideShow"
"1": "razsorMobile"
"2": "carDealerStockView"
"3": "associatedVehiclesCars"
"4": "razsorEnhancedWebsite"
"5": "standOutCars"
}
"additionalLinks": ""
"promotionBullets": ""
"backgroundColourBikes": "Blue"
"backgroundColourCars": "Blue"
"brandingImage": "header.jpg"
"addressDisplayable": "true"
"franchises": ""
"structuredLocation":
{
"county":
{
"region":
{
"numberOfAdverts": 0
"name": "WEST MIDLANDS"
}
"numberOfAdverts": 0
"name": "WEST MIDLANDS"
}
"numberOfAdverts": 0
"name": "BIRMINGHAM"
}
"mobileWebsiteLink": "http://dmsgateway.autotrader.co.uk/api/advertiser/490131/redirect/advert/source/autotrader-desktop/autotrader-advert/201401311453322?website-link=http%3A%2F%2Fwww.parkwaybirmingham.co.uk"
"numberOfReviews": "null"
}
"capTechSpecFlag": "Y"
}
}
"totalResults": 2118
}
"paginator":
{
"currentPage": 1
"totalResults": 2118
"pageSet":
{
"0":
{
"displayName": "1"
"value": "1"
"selected": "true"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=1"
}
"1":
{
"displayName": "2"
"value": "2"
"selected": "false"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=2"
}
"2":
{
"displayName": "3"
"value": "3"
"selected": "false"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=3"
}
"3":
{
"displayName": "First"
"value": "1"
"selected": "true"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=1"
}
"4":
{
"displayName": "Next"
"value": "2"
"selected": "false"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=2"
}
"5":
{
"displayName": "Last"
"value": "2118"
"selected": "false"
"link": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1&Page_Number=2118"
}
}
"resultsPerPage": 1
}
"sortOrderInfo":
{
"0":
{
"Sort_Key": "Price"
"Sort_Order": "Asc"
}
"1":
{
"Sort_Key": "Price"
"Sort_Order": "Desc"
}
"2":
{
"Sort_Key": "Make"
"Sort_Order": "Asc"
}
"3":
{
"Sort_Key": "Model"
"Sort_Order": "Asc"
}
"4":
{
"Sort_Key": "Mileage"
"Sort_Order": "Asc"
}
"5":
{
"Sort_Key": "PaidFor"
"Sort_Order": "Desc"
}
"6":
{
"Sort_Key": "DealerName"
"Sort_Order": "Asc"
}
"7":
{
"Sort_Key": "DealerName"
"Sort_Order": "Desc"
}
"8":
{
"Sort_Key": "Distance"
"Sort_Order": "Asc"
}
"9":
{
"Sort_Key": "Age"
"Sort_Order": "Asc"
}
"10":
{
"Sort_Key": "Age"
"Sort_Order": "Desc"
}
"11":
{
"Sort_Key": "PlacementDate"
"Sort_Order": "Asc"
}
"12":
{
"Sort_Key": "PlacementDate"
"Sort_Order": "Desc"
}
"13":
{
"Sort_Key": "LocationPrice"
"Sort_Order": "Asc"
}
"14":
{
"Sort_Key": "AgePrice"
"Sort_Order": "Asc"
}
"15":
{
"Sort_Key": "MileagePrice"
"Sort_Order": "Asc"
}
}
"requestInfo":
{
"base-path": "http://sss.gen.tradermedia.net/sss"
"image-base-path": "http://pictures2.autotrader.co.uk"
"editorial-image-base-path": "http://www.autotrader.co.uk"
"request-url": "http://sss.gen.tradermedia.net/sss/classified-adverts?Make=FORD&Model=Fiesta&Colour=Red&Page_Size=1"
}
}
感谢您帮助我们,我需要在帖子中包含的任何更改或事项,只需发表评论。
答案 0 :(得分:2)
由于你所尝试的不太清楚,你可能会看到像
这样的事情JACKSON - JSON解析/生成系统。
或许多其他公用事业。
编写自己的解析器会浪费时间。
从文件读取可能会或可能不会被实用程序覆盖,但您可以处理 - 或者在Apache Commons中可能有一些实用程序。
答案 1 :(得分:0)
以下是如何使用Jackson JSON库读取数据的示例:
// create Jackson mapper
ObjectMapper mapper = new ObjectMapper();
String inputString = "{json goes here...}";
// parse JSON into JsonNode, now you can navigate the data structure
JsonNode json = mapper.readTree(inputString);
// get node
JsonNode searchResultsNode = json.get("searchResults");
// get another node from that node
JsonNode classifiedAdvertsNode = searchResultsNode.get("classifiedAdverts");
// get it's value as string
String someValue = classifiedAdvertsNode.asText();
如果您需要阅读文件,只需将文件传递到mapper.readTree(inputString)
,它也可以正常工作。