Cakephp Json输出令人困惑

时间:2015-05-23 20:23:02

标签: java php json xml cakephp

我正在使用Cakephp 2.6,我在理解我正在获得的JSON输出的结构时遇到了问题。我的目标是使用改进库和GSON的Android应用程序。

基本上我有或者想要一个练习对象的ArrayList。每个Exercise对象包含Word和Phrase数组。

CakePHP的: 我的控制器中用于获取锻炼的代码是;

$exercises = $this->Exercise->find('all');

  $this->set(array(
        'exercises' => $exercises,
        '_serialize' => array('exercises')
    ));

然而,Cakephp输出的JSON非常令人困惑或看起来不对吗?有没有办法清理JSON或XML,或者没问题。

JSON

{
    "exercises": [
        {
            "Exercise": {
                "id": "33",
                "title": "jghjhg jhgjhggh for bob",
                "description": "",
                "user_id": "50",
                "patient_id": "1",
                "created": "2015-04-08 12:13:46",
                "modified": "2015-05-14 22:21:35"
            },
            "Word": [
                {
                    "id": "2",
                    "word": "computer",
                    "word_syllables": "com-pute-r",
                    "picture": "computer.jpg",
                    "picture_dir": "2",
                    "soundfile": "",
                    "user_id": "0",
                    "created": "2015-04-08 16:32:07",
                    "modified": "2015-05-01 22:05:15",
                    "fullimagepath": "\/files\/word\/picture\/2\/computer.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/2\/128x128_computer.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/2\/64x64_computer.jpg",
                    "fullsoundpath": "\/files\/word\/soundfile\/2\/",
                    "ExercisesWord": {
                        "id": "46",
                        "word_id": "2",
                        "exercise_id": "33"
                    }
                }
            ],
            "Phrase": [
                {
                    "id": "3",
                    "phrase": "im hungry",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:12",
                    "modified": "2015-04-04 01:04:15",
                    "fullimagepath": "\/files\/phrase\/picture\/3\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/3\/128x128_",
                    "ExercisesPhrase": {
                        "id": "5",
                        "phrase_id": "3",
                        "exercise_id": "33"
                    }
                },
                {
                    "id": "5",
                    "phrase": "i want a drink",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:37",
                    "modified": "2015-04-02 13:50:38",
                    "fullimagepath": "\/files\/phrase\/picture\/5\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/5\/128x128_",
                    "ExercisesPhrase": {
                        "id": "6",
                        "phrase_id": "5",
                        "exercise_id": "33"
                    }
                }
            ]
        },
        {
            "Exercise": {
                "id": "34",
                "title": "test 2",
                "description": "test",
                "user_id": "50",
                "patient_id": "1",
                "created": "2015-05-15 19:30:45",
                "modified": "2015-05-15 19:30:45"
            },
            "Word": [
                {
                    "id": "5",
                    "word": "elephant",
                    "word_syllables": "elephant",
                    "picture": "elephant.jpg",
                    "picture_dir": "5",
                    "soundfile": "",
                    "user_id": "0",
                    "created": "2015-04-08 16:32:07",
                    "modified": "2015-05-09 14:59:49",
                    "fullimagepath": "\/files\/word\/picture\/5\/elephant.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/5\/128x128_elephant.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/5\/64x64_elephant.jpg",
                    "fullsoundpath": "\/files\/word\/soundfile\/5\/",
                    "ExercisesWord": {
                        "id": "47",
                        "word_id": "5",
                        "exercise_id": "34"
                    }
                }
            ],
            "Phrase": [
                {
                    "id": "4",
                    "phrase": "where are we?",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:27",
                    "modified": "2015-04-02 13:50:38",
                    "fullimagepath": "\/files\/phrase\/picture\/4\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/4\/128x128_",
                    "ExercisesPhrase": {
                        "id": "7",
                        "phrase_id": "4",
                        "exercise_id": "34"
                    }
                }
            ]
        },
        {
            "Exercise": {
                "id": "35",
                "title": "retrdtgf",
                "description": "fdgdfg",
                "user_id": "50",
                "patient_id": "1",
                "created": "2015-05-18 20:33:19",
                "modified": "2015-05-18 20:33:19"
            },
            "Word": [
                {
                    "id": "2",
                    "word": "computer",
                    "word_syllables": "com-pute-r",
                    "picture": "computer.jpg",
                    "picture_dir": "2",
                    "soundfile": "",
                    "user_id": "0",
                    "created": "2015-04-08 16:32:07",
                    "modified": "2015-05-01 22:05:15",
                    "fullimagepath": "\/files\/word\/picture\/2\/computer.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/2\/128x128_computer.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/2\/64x64_computer.jpg",
                    "fullsoundpath": "\/files\/word\/soundfile\/2\/",
                    "ExercisesWord": {
                        "id": "48",
                        "word_id": "2",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "5",
                    "word": "elephant",
                    "word_syllables": "elephant",
                    "picture": "elephant.jpg",
                    "picture_dir": "5",
                    "soundfile": "",
                    "user_id": "0",
                    "created": "2015-04-08 16:32:07",
                    "modified": "2015-05-09 14:59:49",
                    "fullimagepath": "\/files\/word\/picture\/5\/elephant.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/5\/128x128_elephant.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/5\/64x64_elephant.jpg",
                    "fullsoundpath": "\/files\/word\/soundfile\/5\/",
                    "ExercisesWord": {
                        "id": "49",
                        "word_id": "5",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "43",
                    "word": "Baby",
                    "word_syllables": "",
                    "picture": "81.jpg",
                    "picture_dir": "43",
                    "soundfile": "100grand.mp3",
                    "user_id": "50",
                    "created": "2015-04-20 18:23:35",
                    "modified": "2015-04-20 19:37:37",
                    "fullimagepath": "\/files\/word\/picture\/43\/81.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/43\/128x128_81.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/43\/64x64_81.jpg",
                    "fullsoundpath": "\/files\/word\/soundfile\/43\/100grand.mp3",
                    "ExercisesWord": {
                        "id": "50",
                        "word_id": "43",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "46",
                    "word": "space",
                    "word_syllables": "",
                    "picture": "5540-observation-deck-wallpaper.jpg",
                    "picture_dir": "46",
                    "soundfile": null,
                    "user_id": "50",
                    "created": "2015-04-20 19:19:53",
                    "modified": "2015-05-14 22:29:56",
                    "fullimagepath": "\/files\/word\/picture\/46\/5540-observation-deck-wallpaper.jpg",
                    "fullimagethumbpath128": "\/files\/word\/picture\/46\/128x128_5540-observation-deck-wallpaper.jpg",
                    "fullimagethumbpath64": "\/files\/word\/picture\/46\/64x64_5540-observation-deck-wallpaper.jpg",
                    "fullsoundpath": null,
                    "ExercisesWord": {
                        "id": "51",
                        "word_id": "46",
                        "exercise_id": "35"
                    }
                }
            ],
            "Phrase": [
                {
                    "id": "2",
                    "phrase": "where are my shoes?",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "30",
                    "created": "2015-03-30 19:27:21",
                    "modified": "2015-04-02 13:50:38",
                    "fullimagepath": "\/files\/phrase\/picture\/2\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/2\/128x128_",
                    "ExercisesPhrase": {
                        "id": "8",
                        "phrase_id": "2",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "3",
                    "phrase": "im hungry",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:12",
                    "modified": "2015-04-04 01:04:15",
                    "fullimagepath": "\/files\/phrase\/picture\/3\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/3\/128x128_",
                    "ExercisesPhrase": {
                        "id": "9",
                        "phrase_id": "3",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "4",
                    "phrase": "where are we?",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:27",
                    "modified": "2015-04-02 13:50:38",
                    "fullimagepath": "\/files\/phrase\/picture\/4\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/4\/128x128_",
                    "ExercisesPhrase": {
                        "id": "10",
                        "phrase_id": "4",
                        "exercise_id": "35"
                    }
                },
                {
                    "id": "5",
                    "phrase": "i want a drink",
                    "description": "",
                    "picture": "",
                    "picture_dir": "",
                    "user_id": "40",
                    "created": "2015-04-02 10:35:37",
                    "modified": "2015-04-02 13:50:38",
                    "fullimagepath": "\/files\/phrase\/picture\/5\/",
                    "fullimagethumbpath128": "\/files\/phrase\/picture\/5\/128x128_",
                    "ExercisesPhrase": {
                        "id": "11",
                        "phrase_id": "5",
                        "exercise_id": "35"
                    }
                }
            ]
        }
    ]
}

XML:

<response>
<exercises>
<Exercise>
<id>33</id>
<title>jghjhg jhgjhggh for bob</title>
<description/>
<user_id>50</user_id>
<patient_id>1</patient_id>
<created>2015-04-08 12:13:46</created>
<modified>2015-05-14 22:21:35</modified>
</Exercise>
<Word>
<id>2</id>
<word>computer</word>
<word_syllables>com-pute-r</word_syllables>
<picture>computer.jpg</picture>
<picture_dir>2</picture_dir>
<soundfile/>
<user_id>0</user_id>
<created>2015-04-08 16:32:07</created>
<modified>2015-05-01 22:05:15</modified>
<fullimagepath>/files/word/picture/2/computer.jpg</fullimagepath>
<fullimagethumbpath128>/files/word/picture/2/128x128_computer.jpg</fullimagethumbpath128>
<fullimagethumbpath64>/files/word/picture/2/64x64_computer.jpg</fullimagethumbpath64>
<fullsoundpath>/files/word/soundfile/2/</fullsoundpath>
<ExercisesWord>
<id>46</id>
<word_id>2</word_id>
<exercise_id>33</exercise_id>
</ExercisesWord>
</Word>
<Phrase>
<id>3</id>
<phrase>im hungry</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:12</created>
<modified>2015-04-04 01:04:15</modified>
<fullimagepath>/files/phrase/picture/3/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/3/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>5</id>
<phrase_id>3</phrase_id>
<exercise_id>33</exercise_id>
</ExercisesPhrase>
</Phrase>
<Phrase>
<id>5</id>
<phrase>i want a drink</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:37</created>
<modified>2015-04-02 13:50:38</modified>
<fullimagepath>/files/phrase/picture/5/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/5/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>6</id>
<phrase_id>5</phrase_id>
<exercise_id>33</exercise_id>
</ExercisesPhrase>
</Phrase>
</exercises>
<exercises>
<Exercise>
<id>34</id>
<title>test 2</title>
<description>test</description>
<user_id>50</user_id>
<patient_id>1</patient_id>
<created>2015-05-15 19:30:45</created>
<modified>2015-05-15 19:30:45</modified>
</Exercise>
<Word>
<id>5</id>
<word>elephant</word>
<word_syllables>elephant</word_syllables>
<picture>elephant.jpg</picture>
<picture_dir>5</picture_dir>
<soundfile/>
<user_id>0</user_id>
<created>2015-04-08 16:32:07</created>
<modified>2015-05-09 14:59:49</modified>
<fullimagepath>/files/word/picture/5/elephant.jpg</fullimagepath>
<fullimagethumbpath128>/files/word/picture/5/128x128_elephant.jpg</fullimagethumbpath128>
<fullimagethumbpath64>/files/word/picture/5/64x64_elephant.jpg</fullimagethumbpath64>
<fullsoundpath>/files/word/soundfile/5/</fullsoundpath>
<ExercisesWord>
<id>47</id>
<word_id>5</word_id>
<exercise_id>34</exercise_id>
</ExercisesWord>
</Word>
<Phrase>
<id>4</id>
<phrase>where are we?</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:27</created>
<modified>2015-04-02 13:50:38</modified>
<fullimagepath>/files/phrase/picture/4/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/4/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>7</id>
<phrase_id>4</phrase_id>
<exercise_id>34</exercise_id>
</ExercisesPhrase>
</Phrase>
</exercises>
<exercises>
<Exercise>
<id>35</id>
<title>retrdtgf</title>
<description>fdgdfg</description>
<user_id>50</user_id>
<patient_id>1</patient_id>
<created>2015-05-18 20:33:19</created>
<modified>2015-05-18 20:33:19</modified>
</Exercise>
<Word>
<id>2</id>
<word>computer</word>
<word_syllables>com-pute-r</word_syllables>
<picture>computer.jpg</picture>
<picture_dir>2</picture_dir>
<soundfile/>
<user_id>0</user_id>
<created>2015-04-08 16:32:07</created>
<modified>2015-05-01 22:05:15</modified>
<fullimagepath>/files/word/picture/2/computer.jpg</fullimagepath>
<fullimagethumbpath128>/files/word/picture/2/128x128_computer.jpg</fullimagethumbpath128>
<fullimagethumbpath64>/files/word/picture/2/64x64_computer.jpg</fullimagethumbpath64>
<fullsoundpath>/files/word/soundfile/2/</fullsoundpath>
<ExercisesWord>
<id>48</id>
<word_id>2</word_id>
<exercise_id>35</exercise_id>
</ExercisesWord>
</Word>
<Word>
<id>5</id>
<word>elephant</word>
<word_syllables>elephant</word_syllables>
<picture>elephant.jpg</picture>
<picture_dir>5</picture_dir>
<soundfile/>
<user_id>0</user_id>
<created>2015-04-08 16:32:07</created>
<modified>2015-05-09 14:59:49</modified>
<fullimagepath>/files/word/picture/5/elephant.jpg</fullimagepath>
<fullimagethumbpath128>/files/word/picture/5/128x128_elephant.jpg</fullimagethumbpath128>
<fullimagethumbpath64>/files/word/picture/5/64x64_elephant.jpg</fullimagethumbpath64>
<fullsoundpath>/files/word/soundfile/5/</fullsoundpath>
<ExercisesWord>
<id>49</id>
<word_id>5</word_id>
<exercise_id>35</exercise_id>
</ExercisesWord>
</Word>
<Word>
<id>43</id>
<word>Baby</word>
<word_syllables/>
<picture>81.jpg</picture>
<picture_dir>43</picture_dir>
<soundfile>100grand.mp3</soundfile>
<user_id>50</user_id>
<created>2015-04-20 18:23:35</created>
<modified>2015-04-20 19:37:37</modified>
<fullimagepath>/files/word/picture/43/81.jpg</fullimagepath>
<fullimagethumbpath128>/files/word/picture/43/128x128_81.jpg</fullimagethumbpath128>
<fullimagethumbpath64>/files/word/picture/43/64x64_81.jpg</fullimagethumbpath64>
<fullsoundpath>/files/word/soundfile/43/100grand.mp3</fullsoundpath>
<ExercisesWord>
<id>50</id>
<word_id>43</word_id>
<exercise_id>35</exercise_id>
</ExercisesWord>
</Word>
<Word>
<id>46</id>
<word>space</word>
<word_syllables/>
<picture>5540-observation-deck-wallpaper.jpg</picture>
<picture_dir>46</picture_dir>
<soundfile/>
<user_id>50</user_id>
<created>2015-04-20 19:19:53</created>
<modified>2015-05-14 22:29:56</modified>
<fullimagepath>
/files/word/picture/46/5540-observation-deck-wallpaper.jpg
</fullimagepath>
<fullimagethumbpath128>
/files/word/picture/46/128x128_5540-observation-deck-wallpaper.jpg
</fullimagethumbpath128>
<fullimagethumbpath64>
/files/word/picture/46/64x64_5540-observation-deck-wallpaper.jpg
</fullimagethumbpath64>
<fullsoundpath/>
<ExercisesWord>
<id>51</id>
<word_id>46</word_id>
<exercise_id>35</exercise_id>
</ExercisesWord>
</Word>
<Phrase>
<id>2</id>
<phrase>where are my shoes?</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>30</user_id>
<created>2015-03-30 19:27:21</created>
<modified>2015-04-02 13:50:38</modified>
<fullimagepath>/files/phrase/picture/2/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/2/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>8</id>
<phrase_id>2</phrase_id>
<exercise_id>35</exercise_id>
</ExercisesPhrase>
</Phrase>
<Phrase>
<id>3</id>
<phrase>im hungry</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:12</created>
<modified>2015-04-04 01:04:15</modified>
<fullimagepath>/files/phrase/picture/3/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/3/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>9</id>
<phrase_id>3</phrase_id>
<exercise_id>35</exercise_id>
</ExercisesPhrase>
</Phrase>
<Phrase>
<id>4</id>
<phrase>where are we?</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:27</created>
<modified>2015-04-02 13:50:38</modified>
<fullimagepath>/files/phrase/picture/4/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/4/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>10</id>
<phrase_id>4</phrase_id>
<exercise_id>35</exercise_id>
</ExercisesPhrase>
</Phrase>
<Phrase>
<id>5</id>
<phrase>i want a drink</phrase>
<description/>
<picture/>
<picture_dir/>
<user_id>40</user_id>
<created>2015-04-02 10:35:37</created>
<modified>2015-04-02 13:50:38</modified>
<fullimagepath>/files/phrase/picture/5/</fullimagepath>
<fullimagethumbpath128>/files/phrase/picture/5/128x128_</fullimagethumbpath128>
<ExercisesPhrase>
<id>11</id>
<phrase_id>5</phrase_id>
<exercise_id>35</exercise_id>
</ExercisesPhrase>
</Phrase>
</exercises>
</response>

Cakephp创建的输出是否正确?当我将一个结果粘贴到JSONtoPOJO站点时,它会生成两个练习对象吗?

http://www.jsonschema2pojo.org/

1 个答案:

答案 0 :(得分:1)