0

I have a url which returns the following, how can I save the data in 3 arrays (Item1Prices[], Item2Prices[], Categories[]) on android? The item prices correspond with the months in the "categories" array. I'm following this tutorial but I need help in modifying the logic in onResponse() to work with mine.

I've seen some examples of this done but I am having trouble recognizing what is what for my case specifically. If someone were to help me get started I would be fine afterwards.

{
  "dataset": [
    {
      "seriesname": "Item1 Price",
      "data": [
        {
          "value": 4.72
        },
        {
          "value": 2.81
        },
        {
          "value": 6.18
        },
        {
          "value": 5.17
        },
        {
          "value": 2.94
        },
        {
          "value": 3.77
        },
        {
          "value": 1.7
        },
        {
          "value": 6.72
        },
        {
          "value": 4.61
        }
      ]
    },
    {
      "seriesname": "Item2 Price",
      "data": [
        {
          "value": 2.49
        },
        {
          "value": 0.72
        },
        {
          "value": 4.06
        },
        {
          "value": 1.74
        },
        {
          "value": 7.23
        },
        {
          "value": 5.83
        },
        {
          "value": 2.59
        },
        {
          "value": 7.54
        },
        {
          "value": 7.02
        }
      ]
    }
  ],
  "categories": [
    {
      "label": "Jan"
    },
    {
      "label": "Feb"
    },
    {
      "label": "Mar"
    },
    {
      "label": "Apr"
    },
    {
      "label": "May"
    },
    {
      "label": "Jun"
    },
    {
      "label": "Jul"
    },
    {
      "label": "Aug"
    },
    {
      "label": "Sept"
    }
  ]
}
Tirth Rami
  • 271
  • 4
  • 15
  • @Boardy I've seen some examples like these but I am trouble figuring out what is what for mine specifically because I haven't really worked with JSON at all before. – Tirth Rami Jan 03 '17 at 22:07
  • Hopefully https://www.tutorialspoint.com/android/android_json_parser.htm can help. Its a full tutorial for JSON and Java/Android. The question is too broad to help, if you get started in getting to the bottom of that tutorial and if you get stuck working the examples with your code then you can ammend your question to get more help. – Boardy Jan 03 '17 at 22:13

1 Answers1

0

I would go with Gson for the parsing.

That way you build annotated POJOs like:

public class MyData{
  @Expose @SerializedName("dataset") ArrayList<Series> dataSet;
  @Expose @SerializedName("categories") ArrayList<Category> categoryList;
}

and

public class Series {
  @Expose @SerializedName("seriesname") String seriesName;
  @Expose @SerializedName("data") ArrayList<DataValue> data;
}

and

public class DataValue{
  @Expose @SerializedName("data") float dataValue;
}

and

public class Category{
  @Expose @SerializedName("label") String label;
}

Then when you need to parse you use:

Gson gson = new GsonBuilder().create();
MyData data = gson.fromJson(jsonString, MyData.class);
Cory Roy
  • 5,379
  • 2
  • 28
  • 47