我想将此字符串转换为android中的json对象?

时间:2012-11-07 05:46:28

标签: android json

字符串是

"categories": {
    "1": {
        "Id": "4",
        "name": "hm\u00c3\u0080\u00c2\u00afI\u00c3\u0084",
        "0": {
            "Id": "250",
            "name": "bpsI"
        },
        "1": {
            "Id": "251",
            "name": "tIcfw"
        },
        "2": {
            "Id": "252",
            "name": "C\u00c2\u00b4y"
        },
        "3": {
            "Id": "253",
            "name": "C\u00c2\u00bdnt{Kj\u00c2\u00b3"
        },
        "4": {
            "Id": "254",
            "name": "hntZiw"
        },
        "5": {
            "Id": "255",
            "name": "{]hmkn"
        },
        "6": {
            "Id": "256",
            "name": "Ncaw"
        },
        "7": {
            "Id": "294",
            "name": "FUnt\u00c3\u00a4mdnb\u00c3\u00b0"
        }
    },
    "2": {
        "Id": "257",
        "name": "Atkmkntbj\u00c2\u00b3"
    },
    "3": {
        "Id": "258",
        "name": "kv]ncnNz\u00c3\u00b0"
    },
    "4": {
        "Id": "259",
        "name": "tlm\u00c2\u00abv Uo\u00c3\u00b0kv",
        "0": {
            "Id": "308",
            "name": "t{Kmkdn"
        },
        "1": {
            "Id": "310",
            "name": "CeIvt{SmWnIvkv"
        },
        "2": {
            "Id": "311",
            "name": "a\u00c3\u00a4p\u00c3\u0085h"
        }
    },
    "5": {
        "Id": "260",
        "name": "hnt\mZw",
        "0": {
            "Id": "261",
            "name": "kn\na"
        },
        "1": {
            "Id": "262",
            "name": "Nm\\u00c3\u00b0"
        },
        "2": {
            "Id": "263",
            "name": "kvt]m\u00c3\u0080Svkv"
        },
        "3": {
            "Id": "264",
            "name": "ayqkn\u00c2\u00a1v"
        }
    },
    "6": {
        "Id": "265",
        "name": "kvs]jy\u00c3\u00b0kv",
        "0": {
            "Id": "266",
            "name": "hm\u00c2\u00a1v]b\u00c3\u00a4v"
        },
        "1": {
            "Id": "267",
            "name": "Fs\u00c3\u00a2 \mSv"
        },
        "2": {
            "Id": "268",
            "name": "A\phw"
        },
        "3": {
            "Id": "269",
            "name": "{]XnIcWw"
        },
        "4": {
            "Id": "270",
            "name": "{]XnI\u00c3\u0084"
        },
        "5": {
            "Id": "271",
            "name": "Ip\u00c3\u00b3mbva"
        },
        "6": {
            "Id": "272",
            "name": "]cnNbw"
        }
    },
    "7": {
        "Id": "273",
        "name": "tImfwkv",
        "0": {
            "Id": "274",
            "name": "Imgv\u00c2\u00a8I\u00c3\u0084"
        },
        "1": {
            "Id": "275",
            "name": "aWntSm\u00c2\u00a1v"
        },
        "2": {
            "Id": "276",
            "name": "bm{X"
        },
        "3": {
            "Id": "277",
            "name": "FUnt\u00c3\u00a4gvkv"
        },
        "4": {
            "Id": "295",
            "name": "Irjn]mTw"
        }
    },
    "8": {
        "Id": "278",
        "name": "Fgp\u00c2\u00afv",
        "0": {
            "Id": "279",
            "name": "kmlnXyw"
        },
        "1": {
            "Id": "280",
            "name": "Ie"
        },
        "2": {
            "Id": "281",
            "name": "lmkyw"
        },
        "3": {
            "Id": "282",
            "name": "Im\u00c3\u0080\u00c2\u00abq\u00c2\u00ac"
        },
        "4": {
            "Id": "283",
            "name": "^o\u00c2\u00a8\u00c3\u0080"
        },
        "5": {
            "Id": "284",
            "name": "Adnhv"
        }
    },
    "9": {
        "Id": "285",
        "name": "tNmtZym\u00c2\u00afcw",
        "0": {
            "Id": "286",
            "name": "C\u00c2\u00bdnt{Kj\u00c2\u00b3"
        },
        "1": {
            "Id": "287",
            "name": "km\u00c2\u00bc\u00c2\u00afnIw"
        },
        "2": {
            "Id": "288",
            "name": "A\u00c2\u00a1u\u00c3\u00afn\u00c2\u00a7v"
        },
        "3": {
            "Id": "289",
            "name": "\nbaw"
        }
    },
    "10": {
        "Id": "290",
        "name": "BiwkI\u00c3\u0084",
        "0": {
            "Id": "298",
            "name": "hnhml hm\u00c3\u0080jnIw"
        },
        "1": {
            "Id": "299",
            "name": "P\u00c3\u00b2Zn\w"
        },
        "2": {
            "Id": "300",
            "name": "skent{_j\u00c2\u00b3"
        },
        "3": {
            "Id": "301",
            "name": "a\u00c3\u00a4p\u00c3\u0085h"
        },
        "4": {
            "Id": "304",
            "name": "BZyIp\u00c3\u0080_m\"}},"11":{"Id":"291","name":"\u00c2\u00a2mknss^Uvkv","0":{"Id":"302","name":"dnb\u00c3\u00b0Ft\u00c3\u008c\u00c3\u00a4v"},"1":{"Id":"303","name":"hn\u00c3\u00b0\u00c2\u00b8\bv\u00c2\u00a1v"},"2":{"Id":"305","name":"Bhiyap\u00c3\u00afv"},"3":{"Id":"306","name":"D
        ]ImckvacW"},"4":{"Id":"307","name":"Ncahm\u00c3\u0080jnIw"},"5":{"Id":"312","name":"am{
            Sn-tam-Wnb\u00c3\u00b0"}}}}

5 个答案:

答案 0 :(得分:4)

将此字符串赋予JSONObject构造函数的输入参数。

String str="";//Give your json string here
JSONObject object= new JSONObject(str);

注意:要插入"使用转义序列,请按"添加\"

答案 1 :(得分:0)

如果您的字符串以“{”开头,则将整个字符串放入

  JSONObjectjb=new JSONObject(YourString);//it convert into json object

如果你的字符串从这个“[”开始形成,那么把你的整个字符串放在: -

  JSONArray jr=new JSONArray(YourString); //it convert into jsonArray

答案 2 :(得分:0)

只需将字符串作为输入参数传递给JSONObject构造函数。

String test="your json string";
JSONObject object= new JSONObject(test);

您还可以使用Gson

String test = "your json string";
Gson gson = new Gson();
gson.toJson(test);

这是一个免费的开源JSON解析库,可以与Android一起使用,是的,真的很容易!!

答案 3 :(得分:0)

尝试使用此工具验证您的json字符串:http://jsonlint.com/

我认为您提供了无效的JSON字符串。

要将有效的JSON字符串转换为JSON对象,您可以尝试使用;

JSONObject object = new JSONObject("your valid json string here");

答案 4 :(得分:0)

你的json无效你可以在这里查看jsonviewer.stack.hu

你在JsonArray中遇到第10个元素的问题:

"10": {
        "Id": "290",
        "name": "BiwkI\u00c3\u0084",
        "0": {
            "Id": "298",
            "name": "hnhml hm\u00c3\u0080jnIw"
        },
        "1": {
            "Id": "299",
            "name": "P\u00c3\u00b2Zn\w"
        },
        "2": {
            "Id": "300",
            "name": "skent{_j\u00c2\u00b3"
        },
        "3": {
            "Id": "301",
            "name": "a\u00c3\u00a4p\u00c3\u0085h"
        },
        "4": {
            "Id": "304",
            "name": "BZyIp\u00c3\u0080_m/"
          }

    },
如果您删除最后一个"name": "BZyIp\u00c3\u0080_m/",那么

"/"来自此行,那么您将String转换为JSONObject

JSONObject object= new JSONObject(str);

或者这是你的完整有效的json字符串:

{"categories": {
    "1": {
        "Id": "4",
        "name": "hm\u00c3\u0080\u00c2\u00afI\u00c3\u0084",
        "0": {
            "Id": "250",
            "name": "bpsI"
        },
        "1": {
            "Id": "251",
            "name": "tIcfw"
        },
        "2": {
            "Id": "252",
            "name": "C\u00c2\u00b4y"
        },
        "3": {
            "Id": "253",
            "name": "C\u00c2\u00bdnt{Kj\u00c2\u00b3"
        },
        "4": {
            "Id": "254",
            "name": "hntZiw"
        },
        "5": {
            "Id": "255",
            "name": "{]hmkn"
        },
        "6": {
            "Id": "256",
            "name": "Ncaw"
        },
        "7": {
            "Id": "294",
            "name": "FUnt\u00c3\u00a4mdnb\u00c3\u00b0"
        }
    },
    "2": {
        "Id": "257",
        "name": "Atkmkntbj\u00c2\u00b3"
    },
    "3": {
        "Id": "258",
        "name": "kv]ncnNz\u00c3\u00b0"
    },
    "4": {
        "Id": "259",
        "name": "tlm\u00c2\u00abv Uo\u00c3\u00b0kv",
        "0": {
            "Id": "308",
            "name": "t{Kmkdn"
        },
        "1": {
            "Id": "310",
            "name": "CeIvt{SmWnIvkv"
        },
        "2": {
            "Id": "311",
            "name": "a\u00c3\u00a4p\u00c3\u0085h"
        }
    },
    "5": {
        "Id": "260",
        "name": "hnt\mZw",
        "0": {
            "Id": "261",
            "name": "kn\na"
        },
        "1": {
            "Id": "262",
            "name": "Nm\\u00c3\u00b0"
        },
        "2": {
            "Id": "263",
            "name": "kvt]m\u00c3\u0080Svkv"
        },
        "3": {
            "Id": "264",
            "name": "ayqkn\u00c2\u00a1v"
        }
    },
    "6": {
        "Id": "265",
        "name": "kvs]jy\u00c3\u00b0kv",
        "0": {
            "Id": "266",
            "name": "hm\u00c2\u00a1v]b\u00c3\u00a4v"
        },
        "1": {
            "Id": "267",
            "name": "Fs\u00c3\u00a2 \mSv"
        },
        "2": {
            "Id": "268",
            "name": "A\phw"
        },
        "3": {
            "Id": "269",
            "name": "{]XnIcWw"
        },
        "4": {
            "Id": "270",
            "name": "{]XnI\u00c3\u0084"
        },
        "5": {
            "Id": "271",
            "name": "Ip\u00c3\u00b3mbva"
        },
        "6": {
            "Id": "272",
            "name": "]cnNbw"
        }
    },
    "7": {
        "Id": "273",
        "name": "tImfwkv",
        "0": {
            "Id": "274",
            "name": "Imgv\u00c2\u00a8I\u00c3\u0084"
        },
        "1": {
            "Id": "275",
            "name": "aWntSm\u00c2\u00a1v"
        },
        "2": {
            "Id": "276",
            "name": "bm{X"
        },
        "3": {
            "Id": "277",
            "name": "FUnt\u00c3\u00a4gvkv"
        },
        "4": {
            "Id": "295",
            "name": "Irjn]mTw"
        }
    },
    "8": {
        "Id": "278",
        "name": "Fgp\u00c2\u00afv",
        "0": {
            "Id": "279",
            "name": "kmlnXyw"
        },
        "1": {
            "Id": "280",
            "name": "Ie"
        },
        "2": {
            "Id": "281",
            "name": "lmkyw"
        },
        "3": {
            "Id": "282",
            "name": "Im\u00c3\u0080\u00c2\u00abq\u00c2\u00ac"
        },
        "4": {
            "Id": "283",
            "name": "^o\u00c2\u00a8\u00c3\u0080"
        },
        "5": {
            "Id": "284",
            "name": "Adnhv"
        }
    },
    "9": {
        "Id": "285",
        "name": "tNmtZym\u00c2\u00afcw",
        "0": {
            "Id": "286",
            "name": "C\u00c2\u00bdnt{Kj\u00c2\u00b3"
        },
        "1": {
            "Id": "287",
            "name": "km\u00c2\u00bc\u00c2\u00afnIw"
        },
        "2": {
            "Id": "288",
            "name": "A\u00c2\u00a1u\u00c3\u00afn\u00c2\u00a7v"
        },
        "3": {
            "Id": "289",
            "name": "\nbaw"
        }
    },
"10": {
        "Id": "290",
        "name": "BiwkI\u00c3\u0084",
        "0": {
            "Id": "298",
            "name": "hnhml hm\u00c3\u0080jnIw"
        },
        "1": {
            "Id": "299",
            "name": "P\u00c3\u00b2Zn\w"
        },
        "2": {
            "Id": "300",
            "name": "skent{_j\u00c2\u00b3"
        },
        "3": {
            "Id": "301",
            "name": "a\u00c3\u00a4p\u00c3\u0085h"
        },
        "4": {
            "Id": "304",
            "name": "BZyIp\u00c3\u0080_m"
          }

    },
"11":{"Id":"291","name":"\u00c2\u00a2mknss^Uvkv",
"0":{"Id":"302","name":"dnb\u00c3\u00b0Ft\u00c3\u008c\u00c3\u00a4v"},
"1":{"Id":"303","name":"hn\u00c3\u00b0\u00c2\u00b8\bv\u00c2\u00a1v"},
"2":{"Id":"305","name":"Bhiyap\u00c3\u00afv"},
"3":{"Id":"306","name":"D]ImckvacW"},
"4":{"Id":"307","name":"Ncahm\u00c3\u0080jnIw"},
"5":{"Id":"312","name":"am{Sn-tam-Wnb\u00c3\u00b0"}
},
}
}