-1

The JSON is in this format

....
     "dummy": {
            "e": "numeric",
            "e2": "dum",
            "e3": "numeric",
            "e4": "numeric"
        },
        ,
        "name":[
        {
              "f1":"a",
              "f2":"b",
        }
      ],
....

I want to get the value of f1 or f2

Java code:

 try {
  JSONObject Object = new JSONObject(json);
  JSONArray Array2 = rootObject2.getJSONArray("name");
  for (int i=0; i<Array2.length(); i++)
{
      JSONObject venueJson2 = dataArray2.getJSONObject(i);
      venue.setSportTypes(venueJson2.getString("f1"));                     
    }

I have tried to use as JSON array and I get value as null, can anyone say what is the correct format to get the value.

halfer
  • 19,824
  • 17
  • 99
  • 186
penta
  • 2,536
  • 4
  • 25
  • 50
  • 2
    Please post the java code that you have tried. – Andrea Thacker Aug 15 '15 at 16:47
  • Do you already have a specific JSON library like Gson or Jackson? If so, don't you understand how to use them or haven't you chosen some library yet? – Sebastian S Aug 15 '15 at 16:52
  • i dont have them, n i dnt knw them, can nrmal java not handle this ? – penta Aug 15 '15 at 16:52
  • Then take a look at [this answer](http://stackoverflow.com/a/31743324/4014509) If this helps, you might want to close your question as duplicate ;) Otherwise please rephrase it, if there is a specific problem. *Edit:* No, there is no neat vanilla Java way. – Sebastian S Aug 15 '15 at 16:54
  • You are missing the starting `{` character. Without that you have no chance of understanding the JSON. If you restore that character and then study json.org for about 10 minutes, then you might understand what's going on. – Hot Licks Aug 15 '15 at 17:12
  • hi hot licks, i wanted to put dots, sorry i had forgotten – penta Aug 15 '15 at 17:16
  • The JSON is not valid, check the JSON format here, http://jsonlint.com/ – viggy Aug 15 '15 at 17:41
  • viggy, i knw it is not in exact format, i have just given part of it,thats y ... – penta Aug 15 '15 at 17:42

1 Answers1

0

The JSON you posted is not an array that's why you get null. U can try:

HashMap<> foo = <Your JSON>
HashMap<String, String> bar = foo.get("dummy");
//get values from bar
ArrayList<> bar2 = foo.get("name");
HashMap<String, String> bar3 = bar2.get(0)
//get values from bar3

for this to work the 2nd "," before names has to be a typo tho

ThomasS
  • 705
  • 1
  • 11
  • 30