1

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.

7
  • Please post valid JSON. Commented Feb 11, 2016 at 5:28
  • your parsing is completely wrong Commented Feb 11, 2016 at 5:30
  • Vivek, its not....it works, for a single arrays Commented Feb 11, 2016 at 5:31
  • Please post Full and Valid JSON, that's what i mean :) Commented Feb 11, 2016 at 5:32
  • Shoeb, I edited it and did that Commented Feb 11, 2016 at 5:32

2 Answers 2

1

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");
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

so I see you put JSONObject currentQuestions = response.getJSONObject(x);
are u creating another for loop? if so can you include that?
@eli: Yes eli because Subcategory is JSONArray of JSONObject so need to use another loop. do it let me know if getting any issue
@eli: also see my edit answer which may be more useful
I only got the first "text": "Text goes here",
|
0

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);
}

1 Comment

Bro, its already been answered but I appreciate the alternative way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.