JSON对象未显示所有内容

时间:2018-07-22 20:02:40

标签: android json

我正在使用Android Volley从API提取JSON对象。我将使用以下URL作为参考:https://glosbe.com/gapi/translate?from=la&dest=eng&format=json&phrase=puer&pretty=true

基本上,我正在尝试使用此API在拉丁语和英语之间进行转换。我遇到一个问题,当我在应用程序中获取JSON并使用toString()显示时,它没有显示某些内容,即使我在浏览器中将其提取,也无法看到。这是我的浏览器中的样子:

  {
  "result" : "ok",
  "tuc" : [ {
    "phrase" : {
      "text" : "boy",
      "language" : "en"
    },
    "meanings" : [ {
      "language" : "en",
      "text" : "male servant"
    }, {
      "language" : "en",
      "text" : "young male"
    }, {
      "language" : "en",
      "text" : "youth (1)"
    } ],
    "meaningId" : 5190805061708394514,
    "authors" : [ 89651 ]
  }, {
    "phrase" : {
      "text" : "child",
      "language" : "en"
    },
    "meanings" : [ {
      "language" : "en",
      "text" : "a minor"
    } ],
    "meaningId" : -7607542873189716522,
    "authors" : [ 89651 ]
  }, {
    "phrase" : {
      "text" : "lad",
      "language" : "en"
    },
    "meaningId" : -4906708417289394649,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "young",
      "language" : "en"
    },
    "meanings" : [ {
      "language" : "en",
      "text" : "in the early part of life or growth"
    } ],
    "meaningId" : -3502972425310277951,
    "authors" : [ 83058 ]
  }, {
    "phrase" : {
      "text" : "laddie",
      "language" : "en"
    },
    "meaningId" : 8881496802632467440,
    "authors" : [ 36 ]
  }, {
    "phrase" : {
      "text" : "son",
      "language" : "en"
    },
    "meaningId" : -5777550766434985278,
    "authors" : [ 25115 ]
  }, {
    "phrase" : {
      "text" : "teenager",
      "language" : "en"
    },
    "meanings" : [ {
      "language" : "en",
      "text" : "person aged between thirteen and nineteen"
    } ],
    "meaningId" : 3903113562290975250,
    "authors" : [ 83058 ]
  }, {
    "phrase" : {
      "text" : "young man",
      "language" : "en"
    },
    "meaningId" : 8100655201654223005,
    "authors" : [ 1490 ]
  }, {
    "phrase" : {
      "text" : "youth",
      "language" : "en"
    },
    "meanings" : [ {
      "language" : "en",
      "text" : "youth (1)"
    } ],
    "meaningId" : 3228296648002997345,
    "authors" : [ 92945 ]
  }, {
    "phrase" : {
      "text" : "attendant",
      "language" : "en"
    },
    "meaningId" : -215343938140795468,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "children",
      "language" : "en"
    },
    "meaningId" : -7262823696521965327,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "kid",
      "language" : "en"
    },
    "meaningId" : 667611545945379095,
    "authors" : [ 13 ]
  }, {
    "phrase" : {
      "text" : "little son",
      "language" : "en"
    },
    "meaningId" : 7069478616059810263,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "male child",
      "language" : "en"
    },
    "meaningId" : -6364965082259980533,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "servant",
      "language" : "en"
    },
    "meaningId" : 3306664352577272579,
    "authors" : [ 66257 ]
  }, {
    "phrase" : {
      "text" : "slave",
      "language" : "en"
    },
    "meaningId" : 3485776351359445593,
    "authors" : [ 66257 ]
  }, {
    "meanings" : [ {
      "language" : "en",
      "text" : "(in the plural) children"
    }, {
      "language" : "en",
      "text" : "boyhood (ex: <i>in puero</i>, &quot;in his boyhood&quot; or &quot;as a boy&quot;)"
    }, {
      "language" : "en",
      "text" : "a child"
    }, {
      "language" : "en",
      "text" : "a boy"
    }, {
      "language" : "en",
      "text" : "a male servant"
    }, {
      "language" : "en",
      "text" : "a bachelor"
    }, {
      "language" : "en",
      "text" : "vocative singular of puer"
    } ],
    "meaningId" : -1803685626071521178,
    "authors" : [ 1 ]
  } ],
  "phrase" : "puer",
  "from" : "la",
  "dest" : "en",
  "authors" : {
    "66257" : {
      "U" : "",
      "id" : 66257,
      "N" : "Charlton T. Lewis",
      "url" : "https://glosbe.com/source/66257"
    },
    "92945" : {
      "U" : "",
      "id" : 92945,
      "N" : "CLLD Concepticon",
      "url" : "https://glosbe.com/source/92945"
    },
    "1" : {
      "U" : "http://en.wiktionary.org",
      "id" : 1,
      "N" : "en.wiktionary.org",
      "url" : "https://glosbe.com/source/1"
    },
    "89651" : {
      "U" : "",
      "id" : 89651,
      "N" : "GlosbeWordalignmentRnD",
      "url" : "https://glosbe.com/source/89651"
    },
    "1490" : {
      "U" : "http://www.archives.nd.edu/cgi-bin/words.exe",
      "id" : 1490,
      "N" : "Piotr Szelma",
      "url" : "https://glosbe.com/source/1490"
    },
    "83058" : {
      "U" : "",
      "id" : 83058,
      "N" : "Dbnary: Wiktionary as Linguistic Linked Open Data",
      "url" : "https://glosbe.com/source/83058"
    },
    "36" : {
      "U" : "http://freedict.org",
      "id" : 36,
      "N" : "freedict.org",
      "url" : "https://glosbe.com/source/36"
    },
    "25115" : {
      "U" : "",
      "id" : 25115,
      "N" : "GlTrav3",
      "url" : "https://glosbe.com/source/25115"
    },
    "13" : {
      "U" : "http://ro.wiktionary.org",
      "id" : 13,
      "N" : "ro.wiktionary.org",
      "url" : "https://glosbe.com/source/13"
    }
  }
}

当我在应用程序中将其拉起时,它不会显示“短语”部分。这是我使用的代码:

    RequestQueue mRequestQueue;
    Cache cache = new DiskBasedCache(getActivity().getCacheDir(), 1024 * 1024); // 1MB cap
    Network network = new BasicNetwork(new HurlStack());
    mRequestQueue = new RequestQueue(cache, network);
    mRequestQueue.start();

    String url = "https://glosbe.com/gapi/translate?from=la&dest=eng&format=json&phrase=" + e.getText().toString();

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
                   Log.d("JSON", response.toString());
                    try
                    {
                        JSONArray arr = response.getJSONArray("tuc");
                        try
                        {
                            JSONObject phrase = response.getJSONObject("phrase");
                        }
                        catch(JSONException e)
                        {
                            Toast.makeText(getActivity(), "Phrase not found", Toast.LENGTH_LONG).show();
                        }
                        Log.d("JSON", arr.toString());
                        try
                        {
                            JSONArray arr2 = arr.getJSONObject(0).getJSONArray("meanings");
                            definition = arr2.getJSONObject(0).getString("text");
                            t.setText(definition);
                            Toast.makeText(getActivity(), "Got stuff sucessfully", Toast.LENGTH_LONG).show();
                        }
                        catch (JSONException e)
                        {
                            JSONObject phrase = arr.getJSONObject(0);
                            definition = phrase.getString("text");
                            t.setText(definition);
                            Toast.makeText(getActivity(), "Got stuff succesfully with exception", Toast.LENGTH_LONG).show();
                        }
                    }
                    catch(JSONException e)
                    {
                        Log.d("error", e.toString());
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error)
                {
                    Log.d("Error", error.toString());
                }
            });

    MySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);

2 个答案:

答案 0 :(得分:0)

您用来解析的代码与JSON的结构不匹配。

“ tuc”是一个数组,“短语”和“含义”在该数组的对象内部。假设您只想获得第一个短语:

Get-Content "appetest.txt" | Select-Object -Index (10,11,12,13,14,15,16,17,18,19,20) |set-content appetest10_new2.txt

我认为是正确的,但目前我没有设置Android项目来对其进行测试。

答案 1 :(得分:0)

确定,这是在android中解析您的响应的代码:

                    try
                    {
                       JSONArray TUCarray = response.getJSONArray("tuc");
                        JsonObject phraseobject=TUCarray.getJSONObject(0).getJSONObject("phrase");
                    }
                    catch(JSONException e)
                    {
                        Toast.makeText(getActivity(), "Phrase not found",Toast.LENGTH_LONG).show();
                    }

在解析整个响应时要小心,因为我会看到TUC数组的某些对象没有子对象短语,例如:-您提供的链接的响应中TUC数组的最后一个对象)。