Question

Problem: The methodology behind extracting data from a JSON file and storing it in java for calls.

What I'm attempting to do: I want to extract the data from the JSON file to produce a comparison of the data on screen using Java.

What I've tried: First of all I tried to find resources and information on extracting data from JSON files, but then tried to do it by manipulating the text by hand, but this is obviously not scaleable.

The Code

{
"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"
  }
}

Thanks for you're help guys, any changes or things I need to include in the post, just leave a comment.

No correct solution

OTHER TIPS

Since it's not so clear what you've tried, you might look into things like

JACKSON -- a JSON parsing/generation system.

Or a whole host of other utilities.

Writing your own parser would be a waste of time.

Reading from the file may or may not be covered by the utilities, but you can handle that--or there may be some utility in Apache Commons.

Jackson Reference

Here is an example of how to read data using Jackson JSON library:

    // 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();

If you need to read a file, just pass the file into mapper.readTree(inputString), it will work just as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top