eli
eli

Reputation: 335

How Do I create a Json Array in a Json Array?

Here is my server that I am trying to parse

    [
  {
   "Hello": "How are You?",
"GoodBye": "I will see you later",
"Today": "How is it outside today",
"Date": "what is the date today",
"Weather": "10 degrees",
"Subcategory": [
  {
    "text": "Text goes here",
          }
{
    "text": "Text goes here",
          }
{
    "text": "Text goes here",
          }]
  [
  {
   "Hello": "How are You?",
"GoodBye": "I will see you later",
"Today": "How is it outside today",
"Date": "what is the date today",
"Weather": "10 degrees",
"Subcategory2": [
{
    "text": "Text goes here",
          }
{
    "text": "Text goes here",
          }
{
    "text": "Text goes here",
          }]
  [

Additionally, below I have my JSON parse for the text alone, however, I would like to get the text of the weather.

Upvotes: 1

Views: 92

Answers (2)

Chandan kushwaha
Chandan kushwaha

Reputation: 949

Just do like this.

JSONArray array=new JSONArray(Your JSONObject);

Now the above is the Main JSONArray.

JSONArray newArray=new JSONArray();
for(int i=0;i<array.length();i++){
 JSONObject objmain=array.getJSONObject(i);
 JSONObject obj=new JSONObject();
 obj.put("your key","your value");
 newArray.put(obj);
 objmain.put("New array key",newArray);
}

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

text key is inside Subcategory JSONArray, so need to get JSONArray first then get text String from it:

JSONObject currentQuestions = response.getJSONObject(x);
JSONArray arrSubcategory=currentQuestions.optJSONArray("Subcategory");
for (int y = 0; y < arrSubcategory.length(); y++) {
   JSONObject objectSubcategory = arrSubcategory.getJSONObject(y);
   String text = objectSubcategory.optString("text");
}

NOTE:

if Subcategory JSONArray key name is dynamic like Subcategory,Subcategory1,... then do it as:

JSONObject currentQuestions = response.getJSONObject(x);
Iterator<String> iter = currentQuestions.keys();
while (iter.hasNext()) {
    String key = iter.next();
    JSONArray arrSubcategory=currentQuestions.optJSONArray(key);
    for (int y = 0; y < arrSubcategory.length(); y++) {
       JSONObject objectSubcategory = arrSubcategory.getJSONObject(y);
       String text = objectSubcategory.optString("text");
    }
}

Upvotes: 1

Related Questions