0

Json解析を取得しています

ここに私のJavaコード

    if(jsonstr!=null){
    try{
        JSONObject   jsonResponse = new JSONObject(jsonstr);

      /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
      /*******  Returns null otherwise.  *******/
      JSONArray jsonproduct = jsonResponse.optJSONArray("products");

      /*********** Process each JSON Node ************/

      int lengthJsonArr = jsonproduct.length();  
      for (int i = 0; i < lengthJsonArr; i++) {
          JSONObject c = jsonproduct.getJSONObject(i);
          itemcode.add(c.get("Code").toString());
          item.add(c.get("Name").toString());
          quantity.add("");
          category.add("CategoryCode");


      }

エラーは

org.json.JSONException: 値 [{"カテゴリ":[{......................すべての json}]

私のjsonは

[ { カテゴリ{[ "a":"s" "b":"g" }, { "a":"s" "b":"g" } ]},

product{[ "a":"s" "b":"g" }, { "a":"s" "b":"g" } ]} ]

MY Json Web サービス URL [Web サービス][1]

この問題を解決する方法を教えてください よろしくお願いします

4

7 に答える 7

1

それを試してください:

に置き換えproductsますcategories

try{
    JSONObject   jsonResponse = new JSONObject(jsonstr);

  /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
  /*******  Returns null otherwise.  *******/
  JSONArray jsonproduct = jsonResponse.optJSONArray("categories");

  /*********** Process each JSON Node ************/

  int lengthJsonArr = jsonproduct.length();  
  for (int i = 0; i < lengthJsonArr; i++) {
      JSONObject c = jsonproduct.getJSONObject(i);
      itemcode.add(c.get("Code").toString());
      item.add(c.get("Name").toString());
      quantity.add("");
      category.add("CategoryCode");


  }
于 2014-05-21T12:29:31.990 に答える
0

使用しているjsonが有効でないためです。あなたがしている解析も有効ではありません

これは、Android jsonの解析から始めるための良いリンクです

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

于 2014-05-21T12:22:40.467 に答える
0

JSON が無効です。

チェックにはオンライン ツールを使用できます: http://json.parser.online.fr/

JSON 構文のリンク: http://www.w3schools.com/json/json_syntax.asp

編集: OK、これで本当の JSON ができました。これは正しいです。

JSONObjectで置き換えるのはどうですかJSONArray。あなたのJSONArray.

于 2014-05-21T12:19:19.797 に答える
0

JSON が無効です。

たぶん、あなたはこのようなものに行きましたか?

{
    "categories": [
        {
            "a": "s",
            "b": "g"
        },
        {
            "a": "s",
            "b": "g"
        }
    ],
    "product": [
        {
            "a": "s",
            "b": "g"
        },
        {
            "a": "s",
            "b": "g"
        }
    ]
}
于 2014-05-21T12:19:26.087 に答える
0

これを試して、

if(jsonstr!=null){
    try{
        JSONArray   jsonResponse = new JSONArray(jsonstr);

 JSONObject   jsonResponse = jsonResponse.get(0);


      /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
      /*******  Returns null otherwise.  *******/
      JSONArray jsonproduct = jsonResponse.getJSONArray("products");

      /*********** Process each JSON Node ************/

      int lengthJsonArr = jsonproduct.length();  
      for (int i = 0; i < lengthJsonArr; i++) {
          JSONObject c = jsonproduct.getJSONObject(i);
          itemcode.add(c.get("Code").toString());
          item.add(c.get("Name").toString());
          quantity.add("");
          category.add("CategoryCode");


      }
于 2014-05-21T12:21:16.327 に答える
0

これは、あなたのjsonが で始まっているために起こってjsonArrayjsonObjectます

[
    {
        "categories": [
            {
                "Code": "1001",
                "Name": "ROOM LINEN",
                "ShortName": "ROLN",
                "DivisionCode": "10",
                "SequenceNo": "3"

@akash moradiya のコードを、彼が指している正しい方法で試してください。

于 2014-05-21T12:24:27.237 に答える