java - How to get a value from a JSON string using jackson library? -


i trying value json string getting null value instead.

app2.java :

package jsonexample1;  import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.map.objectmapper;  import java.io.ioexception; import java.io.stringreader;  public class app2 {      private jsonnode rootnode;      public void setup() throws ioexception {         string jsonstring = "{\n" +                 "                \"hotellistresponse\" : {\n" +                 "                    \"customersessionid\" : \"0abaaa7a-90c9-7491-3ff2-7e2c37496ca2\",\n" +                 "                    \"numberofroomsrequested\" : 1,\n" +                 "                    \"moreresultsavailable\" : true,\n" +                 "                    \"cachekey\" : \"7790c974:13ff7e2c374:6ccd\",\n" +                 "                    \"cachelocation\" : \"10.186.170.122:7300\",\n" +                 "                    \"hotellist\" : {\n" +                 "                        \"@activepropertycount\" : \"223\",\n" +                 "                        \"@size\" : \"1\",\n" +                 "                        \"hotelsummary\" : {\n" +                 "                            \"@order\" : \"0\",\n" +                 "                            \"hotelid\" : 125727,\n" +                 "                            \"name\" : \"red lion hotel on fifth avenue\",\n" +                 "                            \"address1\" : \"1415 5th ave\",\n" +                 "                            \"city\" : \"seattle\",\n" +                 "                            \"stateprovincecode\" : \"wa\",\n" +                 "                            \"postalcode\" : 98101,\n" +                 "                            \"countrycode\" : \"us\",\n" +                 "                            \"airportcode\" : \"sea\",\n" +                 "                            \"suppliertype\" : \"e\",\n" +                 "                            \"hotelrating\" : 3.5,\n" +                 "                            \"propertycategory\" : 1,\n" +                 "                            \"confidencerating\" : 90,\n" +                 "                            \"amenitymask\" : 7847938,\n" +                 "                            \"tripadvisorrating\" : 4,\n" +                 "                            \"locationdescription\" : \"near pike place market\",\n" +                 "                            \"shortdescription\" : \"<p><b>location. </b> <br />red lion hotel on fifth avenue located close 5th avenue theater, pike place market, , washington state convention & trade center. additional points of interest\",\n" +                 "                            \"highrate\" : 149,\n" +                 "                            \"lowrate\" : 126.65,\n" +                 "                            \"ratecurrencycode\" : \"usd\",\n" +                 "                            \"latitude\" : 47.60985,\n" +                 "                            \"longitude\" : -122.33475,\n" +                 "                            \"proximitydistance\" : 11.168453,\n" +                 "                            \"proximityunit\" : \"mi\",\n" +                 "                            \"hotelindestination\" : true,\n" +                 "                            \"thumbnailurl\" : \"/hotels/1000000/60000/51000/50947/50947_180_t.jpg\",\n" +                 "                            \"deeplink\" : \"http://travel.ian.com/index.jsp?pagename=hotavail&cid=55505&hotelid=125727&mode=2&numberofrooms=1&room-0-adult-total=2&room-0-child-total=0&arrivalmonth=8&arrivalday=4&departuremonth=8&departureday=5&showinfo=true&locale=en_us&currencycode=usd\",\n" +                 "                            \"roomratedetailslist\" : {\n" +                 "                                \"roomratedetails\" : {\n" +                 "                                    \"roomtypecode\" : 253461,\n" +                 "                                    \"ratecode\" : 201054304,\n" +                 "                                    \"maxroomoccupancy\" : 2,\n" +                 "                                    \"quotedroomoccupancy\" : 2,\n" +                 "                                    \"minguestage\" : 0,\n" +                 "                                    \"roomdescription\" : \"classic single queen\",\n" +                 "                                    \"promoid\" : 202161947,\n" +                 "                                    \"promodescription\" : \"summer sale! save 15%\",\n" +                 "                                    \"currentallotment\" : 0,\n" +                 "                                    \"propertyavailable\" : true,\n" +                 "                                    \"propertyrestricted\" : false,\n" +                 "                                    \"expediapropertyid\" : 50947,\n" +                 "                                    \"ratekey\" : \"0abaaa7a-90c9-7491-3ff2-7e2c37496cce\",\n" +                 "                                    \"rateinfo\" : {\n" +                 "                                        \"@ratechange\" : \"false\",\n" +                 "                                        \"@promo\" : \"true\",\n" +                 "                                        \"@pricebreakdown\" : \"true\",\n" +                 "                                        \"chargeablerateinfo\" : {\n" +                 "                                            \"@total\" : \"151.23\",\n" +                 "                                            \"@surchargetotal\" : \"24.58\",\n" +                 "                                            \"@nightlyratetotal\" : \"126.65\",\n" +                 "                                            \"@maxnightlyrate\" : \"126.65\",\n" +                 "                                            \"@currencycode\" : \"usd\",\n" +                 "                                            \"@commissionableusdtotal\" : \"126.65\",\n" +                 "                                            \"@averagerate\" : \"126.65\",\n" +                 "                                            \"@averagebaserate\" : \"149.0\",\n" +                 "                                            \"nightlyratesperroom\" : {\n" +                 "                                                \"@size\" : \"1\",\n" +                 "                                                \"nightlyrate\" : {\n" +                 "                                                    \"@promo\" : \"true\",\n" +                 "                                                    \"@rate\" : \"126.65\",\n" +                 "                                                    \"@baserate\" : \"149.0\"\n" +                 "                                                }\n" +                 "                                            },\n" +                 "                                            \"surcharges\" : {\n" +                 "                                                \"@size\" : \"1\",\n" +                 "                                                \"surcharge\" : {\n" +                 "                                                    \"@amount\" : \"24.58\",\n" +                 "                                                    \"@type\" : \"taxandservicefee\"\n" +                 "                                                }\n" +                 "                                            }\n" +                 "                                        }\n" +                 "                                    },\n" +                 "                                    \"valueadds\" : {\n" +                 "                                        \"@size\" : \"1\",\n" +                 "                                        \"valueadd\" : {\n" +                 "                                            \"@id\" : \"2048\",\n" +                 "                                            \"description\" : \"free wireless internet\"\n" +                 "                                        }\n" +                 "                                    }\n" +                 "                                }\n" +                 "                            }\n" +                 "                        }\n" +                 "                    }\n" +                 "                }\n" +                 "            }";          rootnode = new objectmapper().readtree(new stringreader(jsonstring));     }      //other methods     public void basictreemodelread()     {         //just dom, our data in hierarchy of node (in case, jsonnode)         jsonnode afield = rootnode.get("customersessionid");          //the customersessionid has string value         string mystring = afield.astext();          system.out.println("customersessionid is:" + mystring);     } } 

starthere.java:

package jsonexample1;  import java.io.ioexception;  public class starthere {     public static void main(string[] args) {          app2 myapp = new app2();         try {             myapp.setup();         } catch (ioexception e) {             e.printstacktrace();  //to change body of catch statement use file | settings | file templates.         }          myapp.basictreemodelread();      } } 

after debugging found afield value remains null. ideas?

your root node doesn't have customersessionid, has hotellistresponse. first.

//other methods public void basictreemodelread() {     jsonnode innernode = rootnode.get("hotellistresponse"); // element in root node     // element in node     jsonnode afield = innernode.get("customersessionid");      //the customersessionid has string value     string mystring = afield.astext();      system.out.println("customersessionid is:" + mystring); } 

this prints

customersessionid is:0abaaa7a-90c9-7491-3ff2-7e2c37496ca2 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -