[英]How to Get value of filed “cat2” from the json string
How to Get value of filed cat2
from the json
string .......... 如何从json
字符串中获取已归档cat2
值..........
The code I have used to get the json
string is mentioned below... 下面提到了我用于获取json
字符串的代码...
Gson gson = new Gson()
//convert java object to JSON format
String json = gson.toJson(obj);
System.out.println(json);
....console output.......... ....控制台输出..........
{
"feed": [
{
"cat1": "test",
"cat2": "test",
"cat3": "test",
"title": "test"
}
],
"bigimage": 0
}
What should I do further to get the value inside the cat2
ie test
? 我应该进一步做什么才能在cat2
即test
获取值?
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(json);
JsonObject jsonObj = jsonElement.getAsJsonObject();
JsonArray feed = new JsonArray();
feed = jsonObj.getAsJsonArray("feed");
JsonObject jsonOb = feed.get(0).getAsJsonObject();
String value = jsonOb.get("cat2").getAsString();
This is how you can get value
of cat2
. 这就是获取cat2
value
的cat2
。 You will need to add com.google.gson
library to compile this code. 您将需要添加com.google.gson
库来编译此代码。
Add the below code and try: 添加以下代码,然后尝试:
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(data);
JsonObject jsonObj = jsonElement.getAsJsonObject();
JsonArray feed = jsonObj.getAsJsonArray("feed");//get the feed arry from json
JsonObject jsonObject = feed.get(0).getAsJsonObject();//get the first feed element
System.out.println(jsonObject.get("cat2"));//displays test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.