How to read a array value form json in java -


i have following json data:-

{     "store": {         "book": [             {                 "category": "reference",                 "author": "nigel rees",                 "title": "sayings of century",                 "price": 8.95             },             {                 "category": "fiction",                 "author": "evelyn waugh",                 "title": "sword of honour",                 "price": 12.99             },             {                 "category": "fiction",                 "author": "herman melville",                 "title": "moby dick",                 "isbn": "0-553-21311-3",                 "price": 8.99             },             {                 "category": "fiction",                 "author": "j. r. r. tolkien",                 "title": "the lord of rings",                 "isbn": "0-395-19395-8",                 "price": 22.99             }         ],         "bicycle": {             "color": "red",             "price": 19.95         }     },     "expensive": 10 } 

method assert json:-

public void assertjsonvalue (string description, string jsonstring,             string path, object expectedvalue) {          objectmapper objectmapper = new objectmapper();         map<string, object> jsonmap = null;         try {             jsonmap = objectmapper.readvalue(jsonstring,                     new typereference<map<string, object>>() {                     });          } catch (ioexception e) {             fail("could not parse json string:" + jsonstring);         }          object actualvalue = null;         try {             actualvalue = propertyutils.getproperty(jsonmap, path);             system.out.println("actualvalue" + actualvalue);         } catch (illegalaccessexception e) {            // error here         }         assertequals(description, expectedvalue, actualvalue);  } 

when try json value using following, works well.

    assertjsonvalue("bicycle color", json, "store.bicycle.color", "red"); 

i want array value json such details of 1st book.

i have tried following, doesn't me out.

json paths follows:-

  1. "store.book[0]"
  2. "store.book.[0]"
  3. "store.book.category"

how can ?

according this answer, should able mentioned properties this:

assertjsonvalue("...", json, "store.(book)[0].category", "reference"); 

edit:

which version of jackson , beanutils using? i've modified method bit , made simple test case, using beanutils 1.9.2 , jackson 2.6.5 tests seem pass:

import com.fasterxml.jackson.databind.objectmapper; import org.apache.commons.beanutils.propertyutils; import org.junit.test;  import java.io.ioexception;  import static org.junit.assert.*;  public class testjson {      private static final string json = "{\n" +             "    \"store\": {\n" +             "        \"book\": [\n" +             "            {\n" +             "                \"category\": \"reference\",\n" +             "                \"author\": \"nigel rees\",\n" +             "                \"title\": \"sayings of century\",\n" +             "                \"price\": 8.95\n" +             "            }\n" +             "        ],\n" +             "        \"bicycle\": {\n" +             "            \"color\": \"red\",\n" +             "            \"price\": 19.95\n" +             "        }\n" +             "    },\n" +             "    \"expensive\": 10\n" +             "}";      @test     public void testjson() {         asserttrue(assertjsonvalue(json, "store.(book)[0].category", "reference"));         asserttrue(assertjsonvalue(json, "store.(book)[0].author", "nigel rees"));         asserttrue(assertjsonvalue(json, "store.(book)[0].title", "sayings of century"));         asserttrue(assertjsonvalue(json, "store.(book)[0].price", 8.95));     }      public boolean assertjsonvalue(string jsonstring,                                    string path,                                    object expectedvalue) {          objectmapper objectmapper = new objectmapper();         try {             object actual = propertyutils                     .getproperty(objectmapper.readvalue(jsonstring, object.class), path);              if (actual.equals(expectedvalue)) {                 return true;             }          } catch (ioexception | reflectiveoperationexception e) {             // handle error         }         return false;     } } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -