JSON - kłopot z parsowaniem.

0

Cześć, mam takiego JSON-a

"items":
{
(int) subject-class/group id:
{
“nom”: subject's name,
“items”:
[
{
“id”: (int) item's id,
“nom”: (string) item's name,
“text_extra”: (string)
“i_nota”: (double) grade
“supes”: (bool) true if the student failed the test, false otherwise
},
...
]
}
}

I robie to tak
JSONObject itemsObject = jsonObject.getJSONObject("items");
i potem pojawia się problem z tą linijką

(int) subject-class/group id:

bo chce zrobić JSONObject groupObject=itemsObjetc.getJSONObject( // i nie wiem jaka podac tutaj nazwe obiektu ? );

Z góry dzięki za wszelką pomoc!

0

Musiałbyś iterować po tym fragmencie drzewka, a nie szukać.

0

Nie rozumiem za bardzo, mógłbyś jakiś przykładowy fragment kodu wrzucić pokazujący jak "iterować po fragmencie drzewka"(niekoniecznie dla tego przypadku).

0

przecież to nie jest prawidłowy JSON
Wygląda na to, że próbujesz parsować jakąś formę dokumentacji zawartości JSON-a
Ten JSON zapewne miał wyglądać tak:

{
  "items": {
    "<subject-class/group id>": {
      "nom": "subjects name",
      "items": [
        {
          "id": 2342342,
          "nom": "item's name",
          "text_extra": "(string)",
          "i_nota": 0.33,
          "supes": true
        }
      ]
    }
  }
}

Tu masz tool do analizy i edycji JSON-a http://www.jsoneditoronline.org/

0

To jest tylko kawałek tego JSON-a, akurat ten fragment, z którego parsowaniem mam problem.

0
kdmrulez napisał(a):

To jest tylko kawałek tego JSON-a, akurat ten fragment, z którego parsowaniem mam problem.

Ale ten kawałek jest nieprawidłowy pod względem składniowym. Nawet uwzględniając, że to jest fragment to nie jest to fragment JSON-a. Użyte są nawet niedopuszczalne znaki: cudzysłowie z poza zakresu ASCII

Wklej sobie całość do: http://www.jsoneditoronline.org/ i zobacz jako jest to sieczka.

0

Tak, masz tutaj pełną rację. To jest tylko dokumentacja do tego, wybacz nie precyzyjność.ale nadal nie wiem co zrobić z tym <subject-class/group id>, w tym miejscu ten JSON zwraca mi coś takiego,"3139"?

0

I tak ta dokumentacja jest jakaś ułomna. To powinien być xml opisujący format danych serwera, jakis wadl, xsd lub coś innego w tym stylu.

Problem w tym, że zupełnie nie rozumiesz co chcesz zrobić skoro parsujesz dokumentację. Opisz swój problem od początku do końca to może będziemy w stanie coś wyjaśnić.

0

To jest tak, dostałem taka dokumentację do JSON-a i tej części, która wrzuciłem mam z tego fragmentu poniżej:

“items”:
{
“nom”: subject's name,
“items”:
[
{
“id”: (int) item's id,
“nom”: (string) item's name,
“text_extra”: (string)
“i_nota”: (double) grade
“supes”: (bool) true if the student failed the test, false otherwise
},
...
]
}

mam pobrać tablicę zawierająca wszystkie

subject's name,(string) item's name i (double) grade i potem wrzucić to wszystko do listView, które wyświetli mi te dane.

0

To nadal jest coś nie tak, nie wierzę, że ktoś nazwał obiekt 'items' i potem tablicę też 'items'

0

To jest fragment dokumentacji, który posłużyl mi do napisania tego co wrzuce niżej

1. "avaluacions":
[
{
“nom”: school's term's name
“num”: school's term's number (ussuablly between 1 and 3, but could be
bigger)
“actual”: (bool) true if this is the current term. Otherwise this "eld is not
present.
},
...
]
2. "items":
{
(int) subject-class/group id:
{
“nom”: subject's name,
“items”:
[
{
“id”: (int) item's id,
“nom”: (string) item's name,
“text_extra”: (string)
“i_nota”: (double) grade
“supes”: (bool) true if the student failed the test, false otherwise
},
...
]
}
}

I to jest mój kod:

public class DayToDayInitResponse {
    private List<Avaluations> mAvaluations;
    private List<Subject> mSubject;

    public DayToDayInitResponse(String json, Context context) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(json);
            JSONArray avaluacionsTab = jsonObject.getJSONArray("avaluacions");
            mAvaluations = new ArrayList<>();
            for (int i = 0; i < avaluacionsTab.length(); i++) {
                Avaluations avaluations = new Avaluations();
                JSONObject jsonAvaluacions = avaluacionsTab.getJSONObject(i);
                avaluations.setTermName(jsonAvaluacions.getString("nom"));
                avaluations.setTermNumber(Integer.parseInt(jsonAvaluacions.getString("num")));
             
                try {
                    avaluations.setActual(jsonAvaluacions.getBoolean("actual"));
                } catch (Exception ex) {

                }
                mAvaluations.add(avaluations);
            }
//Poniżej tego to co napisałem jest efektem różnych prób i błędów, więc może to nie mieć za dużego sensu
            JSONObject itemsObject = jsonObject.getJSONObject("items");
            Subject subject = new Subject();
            ItemsModel itemsModel = new ItemsModel();

            JSONObject groupObject = itemsObject.getJSONObject("3139");
            JSONArray itemObject = groupObject.getJSONArray("items");
            ArrayList<Item> itemList = (ArrayList) itemsModel.getItemList();
            for (int j = 0; j < itemObject.length(); j++) {
                Item item = new Item();
                item.setItemName(itemObject.getString(1));
                item.setTextExtra(itemObject.getString(2));
                item.setGrade(itemObject.getDouble(3));
                item.setSupes(itemObject.getBoolean(4));
                itemList.add(item);
            }

            subject.setItemsModel(itemsModel);
            mSubject=new ArrayList<>();
            mSubject.add(subject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public List<Avaluations> getAvaluations() {
        return mAvaluations;
    }

    public List<Subject> getSubjects() {
        return mSubject;
    }
}

Wiem, że nie użyłem tej biblioteki, o której mówisz, ale zostawmy to, jeśli będe wiedział jak to zrobić to przepisanie tego kodu, z wykorzystaniem biblioteki, o której wspomniałeś nie będzie problemem.

0

Sory, już rozumiem o co Ci chodzi, zmyliło mnie ona zamiast on. Tutaj wrzucam to co dostaje jako response

{  
   "avaluacions":[  
      {  
         "nom":"1a Avaluaci\u00f3",
         "num":"2",
         "actual":true
      },
      {  
         "nom":"2a Avaluaci\u00f3",
         "num":"3"
      },
      {  
         "nom":"3a Avaluaci\u00f3",
         "num":"1"
      },
      {  
         "nom":"4a Avaluaci\u00f3",
         "num":"4"
      }
   ],
   "items":{  
      "3139":{  
         "nom":"Educaci\u00f3n f\u00edsica",
         "items":[  
            {  
               "id":"1345",
               "nom":"Item 1 EF",
               "text_extra":"",
               "i_nota":"7.00",
               "suspes":false
            }
         ]
      },
      "3140":{  
         "nom":"F\u00edsica",
         "items":[  
            {  
               "id":"1348",
               "nom":"Item 1 F",
               "text_extra":"",
               "i_nota":"5.00",
               "suspes":false
            }
         ]
      },
      "3141":{  
         "nom":"Geograf\u00eda",
         "items":[  
            {  
               "id":"1351",
               "nom":"Item 1 G",
               "text_extra":"",
               "i_nota":"6.00",
               "suspes":false
            }
         ]
      },
      "3142":{  
         "nom":"Historia",
         "items":[  
            {  
               "id":"1354",
               "nom":"Item 1 H",
               "text_extra":"",
               "i_nota":"9.00",
               "suspes":false
            }
         ]
      },
      "3144":{  
         "nom":"Lat\u00edn",
         "items":[  
            {  
               "id":"1360",
               "nom":"Item 1 L",
               "text_extra":"",
               "i_nota":"4.00",
               "suspes":true
            }
         ]
      }
   }
}
0

Ale jest jeszcze jeden problem. Robie to w taki sposób

JSONObject itemsObject = jsonObject.getJSONObject("items");
            mSubjects = new ArrayList<>();
            for (int i = 0; i < itemsObject.length(); i++) {
                Subject subject = new Subject();
                JSONObject object=itemsObject.getJSONObject(i);//I tutaj mi się burzy, dlatego, że oczekuje stringa z nazwa obietku, a skad ja mam go znac?
            }
0

A może tak:

for(Iteraor key= itemsObject.keys();itr.hasNext();) {
   itemsObject.get(key.next());
}

albo na JSONObject wywołaj names(), co zwróci tablię z nazwami obiektów.
http://www.json.org/javadoc/org/json/JSONObject.html#names()

1 użytkowników online, w tym zalogowanych: 0, gości: 1