0

I have a problematic json in which the nodes are variable (urls define the nodes) see "highlighting" part below - I am trying to get the url & fulltxt part from the highlighting node as below - (unsuccessful so far, please help)

    {
    "responseHeader": {
    "status": 0,
    "QTime": 24,
    "params": {
      "fl": "urlFull",
      "indent": "true",
      "q": "hello",
      "_": "1389977088005",
      "hl.simple.pre": "",
      "hl.simple.post": "",
      "hl.fl": "fulltxt",
      "wt": "json",
      "hl": "true"
    }
    },
    "response": {
    "numFound": 3178,
    "start": 0,
    "docs": [
      {
        "urlFull": "http://www.hellodesign.com/"
      },
      {
        "urlFull": "http://kadlot.com/"
      },
      {
        "urlFull": "http://insanityprone.tumblr.com/"
      },
      {
        "urlFull": "http://www.privatevoyeur.com/PSOctober13/am23043/index.html"
      },
      {
        "urlFull": "http://www.privatevoyeur.com/PSOctober13/am23043/qvindex.html"
      },
      {
        "urlFull": "http://seoul-arts.123.st/f268-hello"
      },
      {
        "urlFull": "http://www.edugeek.net/groups/south-west-techs/178-hello/"
      },
      {
        "urlFull": "http://www.gatewaytoroleplay.com/t1271-hello"
      },
      {
        "urlFull": "http://www.4networking.biz/forum/ViewPost/1078797"
      },
      {
        "urlFull": "http://www.blackhatim.com/f18/hello-108347.html"
      }
    ]
    },
    "highlighting": {
    "http://www.hellodesign.com/": {
      "fulltxt": [
        "Hello You need the Latest Flash Player to view this site. Click here to download Flash Click here if you know you have Flash Address 10305 Jefferson Blvd. Culver City, CA 90232 Telephone 310-839-4885",
        " Fax 310-839-4886 Email New Business newbusiness@hellodesign.com Press Inquiries press@hellodesign.com Career Inquiries careers@hellodesign.com General Inquiries hello@hellodesign.com"
      ]
    },
    "http://kadlot.com/": {
      "fulltxt": [
        "Hello! Hello! Sooo... I didn't have much time to give you a prezi with nice graphics, but I thought you could help me out* with the drawings at least! So I made this site you can draw on! Try it and"
      ]
    },
    "http://insanityprone.tumblr.com/": {
      "fulltxt": [
        "Hello... Hello... ~ And welcome to my facilities. Yes, mine. Not Her's. Mine. If you're curious about anything, anything at all, feel free to ask me. I've been here for a while now; I've picked a few",
        " they came into view. Well didn’t they look familiar. Vaguely so. “Hello..” he offered a thin grin, just a sliver of those white teeth, hoping to eminate a false sense of friendliness. While he did like"
      ]
    },
    "http://www.privatevoyeur.com/PSOctober13/am23043/index.html": {
      "fulltxt": [
        "Hello EscapeTrail PrivateVoyeur InstantPost Archive CamFuze PrivateVoyeur Proudly Presents:               Hello  Contributor Story None  From   ...  Comments by   John  Legal Advisory The photos"
      ]
    },
    "http://www.privatevoyeur.com/PSOctober13/am23043/qvindex.html": {
      "fulltxt": [
        "Hello EscapeTrail PrivateVoyeur InstantPost Archive CamFuze PrivateVoyeur Proudly Presents:               Hello  Contributor Story None  Legal Advisory The photos below are copyright protected. You"
      ]
    },
    "http://seoul-arts.123.st/f268-hello": {
      "fulltxt": [
        "HELLO!   ❥S-A-U ::  ANNYEONG! ::  HELLO!  ! & !   Atsakymai   Autorius   Peržiūrėjo   Paskutinis pranešimas  !: MEET  [ Pereiti prie: 1, 2, ... , 5, 6] 83 KIM JAEJOONG 365 Today at 7:27 pm PARK BOM",
        ": Nieko Moderatorius: STARS Permissions in this forum: Jūs negalite rašyti naujų pranešimų į šį forumą Jūs negalite atsakinėti į pranešimus šiame forume  ❥S-A-U :: ANNYEONG! :: HELLO! Vartotojai",
        ", peržiūrintys šį forumą: Nieko  ❥S-A-U :: ANNYEONG! :: HELLO! Pereiti į: Pasirinkite forumą||--ANNYEONG!|   |--HELLO!|   |--TAISYKLĖS|   |--INFORMACINĖ DĖŽUTĖ|   |--NAUJOKO MENIU!|   |--Švieslentė|   |--Būkim"
      ]
    },
    "http://www.edugeek.net/groups/south-west-techs/178-hello/": {
      "fulltxt": [
        "Hello Full Theme Forum Today's Posts Search   Hello jimmy_2k 19th June 2010 10:59 AM Though i'd say hi, i'm from a school in Bristol SW-ICT 18th November 2011 10:21 AM Same!! Started off in Plymouth"
      ]
    },
    "http://www.gatewaytoroleplay.com/t1271-hello": {
      "fulltxt": [
        "Hello !! Gateway to Role-Play A friendly community with various genres and freedom in role-play! Member of the Month & Year: MethosArchaic Hey! Listen! If GtR has 100,000 overall posts on the board",
        " by the end of this year, Leah7 will dye her hair to popular vote, HikariKuragari will dye her hair red, and Venom will dye his hair the color of popular vote on the forum! Hello !! Gateway to Role-Play",
        " :: Earth Realm :: Introductions Page 1 of 2 • Share •  Page 1 of 2 • 1, 2      Hello !!  by Ange Tuteur on Mon Nov 11, 2013 3:56 am Hello I'm Ange Tuteur! I'll be honest I've never ever ever role played! So"
      ]
    },
    "http://www.4networking.biz/forum/ViewPost/1078797": {
      "fulltxt": [
        "Hello\n Remember Me | Password reminder | New Member? Login Member login Shiny new version of #4N Meeting Finder App out now for Apple and Android http://t.co/4KDSZdwZH5 Home What's 4Networking",
        " forum Today's posts Last 24 hours Search forum Articles Meet the 4N Team FREE networking Home > Forum > SAY HELLO! > Hello Hello Matt Reeds Frost Group Limited 0 Member Since: 11th Nov 2013 Telephone",
        ": Today at 04:18 Quote Hello, I am also new is 4networking to find some new frined. View Profile Send Message Leave Testimonial Find Posts   Kat Evans Server Start Ltd 0 Member Since: 11th Nov 2013"
      ]
    },
    "http://www.blackhatim.com/f18/hello-108347.html": {
      "fulltxt": [
        "Hello Register Help Remember Me? Forum Today's Posts FAQ Calendar Community Member List Forum Actions Mark Forums Read Quick Links View Forum Leaders What's New? Advanced Search Forum Blackhatim.com",
        " Off Topic Hello + Reply to Thread Results 1 to 2 of 2 Thread: Hello LinkBack LinkBack URL About LinkBacks   Bookmark & Share Digg this Thread! Add Thread to del.icio.us Bookmark in Technorati Tweet this",
        " #1 greersca Junior Member Join Date Jan 2012 Posts 15 Thanks 0 Thanked 0 Times in 0 Posts Hello Just want to say hello to everyone. I am new here, and just wanted to drop a message. If anyone knows how"
      ]
    }
  },
  "spellcheck": {
    "suggestions": []
  }
}

yes i used gson & jackson, but somehow the values dont come in...the gson class that i created for the bean is -

public class SolrResultItem {
    public Response response;
}

class Response {
    public int numFound;
    public int start;
    public List<Doc> docs;
    public List<Highlighting> highlightingDocs;
}

class Doc {
    public String urlFull;
    public String title;
    public String fulltxt;
    public String relatedItems;
}
class Highlighting {
    public String urlFull;
    public String title;
    //public List<String> title;
    public List<String> fulltxt;
}
TheGeekBETA
  • 33
  • 1
  • 7
  • Are you using a JSON parser library such as on at the bottom of http://json.org/? – philipvr Jan 17 '14 at 17:00
  • yes i used gson & jackson, but somehow the values dont come in...the gson class that i created for the bean is - as added in post above – TheGeekBETA Jan 18 '14 at 01:35
  • Virtually any JSON parser will, by default, map the JSON to a structure of Maps and Lists. Once you have that it's a simple matter to "navigate" through the structure to find whatever you want. You don't need to define a class for everything. – Hot Licks Jan 18 '14 at 02:17
  • 1
    And it's easier to tell what you're doing if you run the JSON through a "pretty printer". http://json.parser.online.fr/ does a fairly good job of this. When you do that you can see that "highlighting" is in the first level "object" (Map) and contains 11 elements, each itself a Map, identified by a URL key. These inner Maps each contain only a single element with a key of "fulltxt" whose value is an "array" (List) containing elements which are character strings. – Hot Licks Jan 18 '14 at 02:26

1 Answers1

0

Using jackson, you can map the "highlighting" attributes to a Map

edit: having see your code, here are the corrections i would do to your classes to use them with jackson:

public class SolrResultItem {
    public ResponseHeader responseHeader;
    public Response response;
    public Map<String,Fulltxt> highlighting;
    public Spellcheck spellcheck;
}

class ResponseHeader{
  public int status;
  public int QTime;//notation discouraged
  public Map<String,String> params;
}

class Response {
    public int numFound;
    public int start;
    public List<Doc> docs;
    public List<Highlighting> highlightingDocs;
}

class Doc {
    public String urlFull;
}
class Fulltxt{
    public List<String> fulltxt;
}

And just to be sure, here is an example for using Jackson

String s=yourjson;
Type type=SolrResultItem.class;
ObjectMapper objectMapper = new ObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructType(type);
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
SolrResultItem result = objectMapper.readValue(s, javaType);

Note that this code is not very clean, we should use private attributes, getters/setters, ang Jackson annotation for the upprecase attribute. hope this helps, you may have some problem with the "Map String,Fulltxt ", your jackson objectMapper has to use it correctly.

pdem
  • 3,570
  • 1
  • 22
  • 36