Home > other >  JSON parsing help (Native Android, java)
JSON parsing help (Native Android, java)

Time:09-17

I am trying to do JSON parsing. The JSON data is shown below, I am trying to get the "categories". I was able to JSON parse everything else, but I am not sure what does this "categories" belong to, is it a JSONObject, JSONArray, or something else? I am a newbie and self-taught, usually I am familiar that JSONArray has form of "JSONArray": {["content"]}, and the "content" is JSONObject. But in this case, "categories":["content"]. I am trying to parse this "categories", and turn it to string. Thank you for your help.

{
   "results": [
   {
      "type": "Restaurant",
      "id": "jfhuiewjkfkdljiahueijkfnlsdiejkl1484391hjk8421k",
      "score": 99.9844207764,
      "dist": 15.581982823437135,
      "info": "search:ta:840369014527642-US",
      "poi": {
         "name": "RoofTop Bar",
         "categorySet": [
         {
            "id": 184729472943
         }
      ],
      "categories": [
         "pub food",
         "restaurant"
      ]}
   }]
}

This is what I have tried:

groups = new JSONArray(); 
groups = response.getJSONArray("results");
if (groups.length() > 0) {
   JSONObject resultObject = groups.getJSONObject(0);
   if (resultObject.has("poi")) {
      if (resultObject.getJSONObject("poi").has("name")) {
         nameResult = resultObject.getJSONObject("poi").getString("name");
      } else {
         nameResult = "Information is not available.";
      }
      if (resultObject.getJSONObject("poi").has("categories")) {
         JSONObject categoriesResult;
         categoriesResult = resultObject.getJSONObject("categories").toString();
      }

CodePudding user response:

  • results is an array of objects
  • The first object contains a property called poi
  • poi contains a property called categories

So using the top to bottom approach, we can arrive at

const categoriesArray = results[0].poi.categories; // gives categories as an array of strings

const categoriesString = categoriesArray.join(",") // gives categories as string, with comma separated values

CodePudding user response:

I am not sure if it is the actual raw data but the poi object where the categories are contained is malformed. It is missing a closing bracket which could be causing parsing issues.

That apart, the field categories from the poi object is a list of strings I am not sure how you want to format it to a string but you could loop through them and do want you want with them.

In order to obtain them you can access them from your object with results[0].poi.categories or loop through the results before accessing the categories with result.poi.categories where result is the variable containing the currently looped result.

EDIT:

From your code sample, assuming response is a JSONObject you can do the following.

Then to obtain categories in a string without the array format, you can loop through the categories and concatenate them to a string.

String categories = resultObject.get("categories").join(", ");
  • Related