0

I am trying to finish up part of my Android application that enables the user to enter a URL, and pull the JSON array data from it. Currently the application takes the users URL, and stores the string as something that looks like this:

{
"name": "Demo",
"currentdistance": "0",
"goals": [
    {
        "goal": {
            "depth": "2",
            "goal": "2"
        },
        "goal": {
            "depth": "6",
            "goal": "4"
        },
        "goal": {
            "depth": "4",
            "goal": "3"
        }
    }
]

}

Now I have found many solutions (such as this) that work great, but they assume that you know ahead of time what the key values are. I am trying to make a JSON array from a source that is not known at compile time, and could range from one key to many.

I have tried just saving the string as a JSON object, then using .put to add it to an array, but that is not much use if the data has more than one key. I have to be able to parse and treat is like a true JSON array once I have gotten the data from the URL.

I am not concerned about error checking on the URL right now, so just assume the URL given to the app is a proper HTTP JSON text like shown above.

Any help would be appreciated!

Community
  • 1
  • 1
SirFerret
  • 61
  • 1
  • 8
  • 1
    http://stackoverflow.com/a/13573965/1979882 to iterate over all keys read this answer – Vyacheslav Jan 27 '16 at 13:31
  • Um, well, if you don't know the names of the keys that you are interested in, how exactly do you know what you are interested in? Forget programming for a moment. What is the algorithm that you, as a ferret, would use to determine what subset of data you want from the JSON? Bear in mind that the JSON can be anything, and so there may be zero, one, or N things that you want. – CommonsWare Jan 27 '16 at 13:32
  • This section of my app here is only a portion of the entire program. The program lets the user connect many "modules" of code and string the inputs and outputs together as to make a much more complicated application. My goal for this module is to get the JSON data from the url into a proper JSON array so that it can be used by other modules of the application. Ex. someone wants to connect another module to the output of my URLtoJSONArray module here. Only the user cares about the keys once they get them, myself as the programmer am not concerned what they are, other than that they are correct. – SirFerret Jan 27 '16 at 13:55

1 Answers1

1

Cannot. If you don't really know JSON schema, you cannot parse it to POJO (Plain Old Java Object) because Java is a strongly-type language. You can parse custom JSON string to object in some dynamic languages such as Javascript.

In case you want to browse all key-value of your json string.Sample code

jObject = new JSONObject(contents.trim());
Iterator<?> keys = jObject.keys();

while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( jObject.get(key) instanceof JSONObject ) {

    }
}

Hope this help :)

Community
  • 1
  • 1
hqt
  • 29,632
  • 51
  • 171
  • 250