-2

I have a JSONObject

{"notes":{
"remarks":["abc","xyz"],
"factory notes":["abc","xyz"],
"name":"jdfb"
}
} 

** i want to insert it to a table as remarks as a string factory notes as a string and name is already a string. but when i iterate this how do i know if it is array or string. whats the condition for that?

for name i could do obj.getString("name"), and for array its getJsonarray. if i can distinguish that i could easily convert it to string and inset to the table.**

String description=null;
                 Iterator<String> notesIterator = notesObj.keys();

                 while (notesIterator.hasNext()) {
                     String notesKey = notesIterator.next();
                     Object notesItrObj=notesIterator.next();
                 
                   
                     if(notesItrObj instanceof String ) {
                         description=notesObj.getString(notesKey);

                     }
                     else {

                      
                         JSONArray
notesArray=notesObj.getJSONArray(notesKey);
                       
                         description=notesArray.toString();
              

      


                 }
Kevin
  • 5
  • 4

1 Answers1

0

The mistake in your code is here:

Object notesItrObj=notesIterator.next();

This should be:

Object notesItrObj=notesObj.get(notesKey);

This is because notesIterator.next() returns a key, not a value.

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45