如何在JAVA中解析此JSON响应
我想解析这些类型的Json响应:如何在JAVA中解析此JSON响应,java,json,Java,Json,我想解析这些类型的Json响应: { "MyResponse": { "count": 3, "listTsm": [{ "id": "b90c6218-73c8-30bd-b532-5ccf435da766", "simpleid": 1, "name": "vignesh1" }, { "id": "b90c6218-73c8-30bd-b532-5ccf435da766", "s
{
"MyResponse": {
"count": 3,
"listTsm": [{
"id": "b90c6218-73c8-30bd-b532-5ccf435da766",
"simpleid": 1,
"name": "vignesh1"
},
{
"id": "b90c6218-73c8-30bd-b532-5ccf435da766",
"simpleid": 2,
"name": "vignesh2"
},
{
"id": "b90c6218-73c8-30bd-b532-5ccf435da766",
"simpleid": 3,
"name": "vignesh3"
}]
}
}
我尝试使用简单的JSON解析器,但这对我不起作用:
Object obj = parser.parse(resp);
JSONObject jsonObject = (JSONObject) obj;
JSONArray response = (JSONArray) jsonObject.get("MyResponse");
//JSONArray arr=new JSONArray(yourJSONresponse);
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<response.size(); i++){
list.add(response.get(i)("name"));
}
objectobj=parser.parse(resp);
JSONObject JSONObject=(JSONObject)对象;
JSONArray响应=(JSONArray)jsonObject.get(“MyResponse”);
//JSONArray arr=新的JSONArray(yourJSONresponse);
ArrayList=新建ArrayList();
对于(int i=0;i
注释:我没有添加验证
[编辑]
加载json字符串的其他方法
JSONObject obj= new JSONObject();
JSONObject jsonObject = obj.fromObject(jsonString);
....
您可以简单地执行以下操作:
JSONObject response = new JSONObject(resp);
然后,您可以根据变量的类型使用以下命令:
int count = response.getint("count");
或
然后,如果你想遍历里面的对象,就只使用一个for。我不认为JSONObject
接受后面的,
逗号。JSON消息不是有效的JSON,这可能是你的问题。我只是用@nickebbitt检查了你的JSON字符串。一些解析器接受,
。没有列表ponse
输入你的json…@SotiriosDelimanolis干杯,不知道我遇到了错误,比如“构造函数JSONObject(字符串)未定义”…JSONObject JSONObject=new JSONObject();JSONObject myResponse=JSONObject.getJSONObject(“myResponse”)你是哪个版本的using@MaximShoustin你能发送直接链接吗?找不到相关结果。
JSONObject response = new JSONObject(resp);
int count = response.getint("count");
JSONArray tsm = response.getJSONArray(listTsm)