Java json对象包含json数组解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18058023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json object contain json array parsing
提问by Aravind Cheekkallur
Am using a function to add list items,name ,id into a jsonobject and returning that object into called function,and getting all the way..But i am little confused with parsing of that json string.
我正在使用一个函数将列表项、名称、id 添加到 jsonobject 中,并将该对象返回到被调用的函数中,然后一路走来......但我对解析那个 json 字符串有点困惑。
JSONObject obj = abcobj.function();
public JSONObject function()
{
jsonobj.put("test",list);
jsonobj.put("name",name);
jsonobj.put("id",id);
return jsonobj;
}
System.out.println("Json object"+devid);
Output
输出
{"test":["test","test1"],"name":"xxxxx","id":"1234"}
please help me to parse this object
请帮我解析这个对象
回答by Jose Sutilo
What exactly do you want to parse? If what you want is to get access to the data inside test1, for example, you need to retrieve test[1] then get the data out of there. If you are wondering why are you seeing the name of the object in the list, my guess is that you have a toString where you are only displaying the name of the object, in the class that you are adding to that list.
你到底想解析什么?例如,如果您想要访问 test1 中的数据,则需要检索 test[1] 然后从那里获取数据。如果您想知道为什么会在列表中看到对象的名称,我的猜测是您有一个 toString,其中您仅在要添加到该列表的类中显示对象的名称。
Another thing to note, that json that you showed to us in the output, is not valid json. Or rather, it has got 2 fields with the same name, which is not recommended as an object can only have 1 field with the same name.
另一件需要注意的事情是,您在输出中向我们展示的 json 不是有效的 json。或者更确切地说,它有 2 个同名的字段,不建议这样做,因为一个对象只能有 1 个同名的字段。
Can you post all the code that you are using for creating and parsing the json? Also, have you considered using GSON? A very simple and powerful JSON library from Google.
您可以发布用于创建和解析 json 的所有代码吗?另外,您是否考虑过使用 GSON?来自 Google 的一个非常简单而强大的 JSON 库。
回答by Avinash
I'm assuming you want to store the interestKeys in a list.
我假设您想将 interestKeys 存储在列表中。
Using the org.jsonlibrary:
使用org.json库:
JSONObject obj = new JSONObject("{interests : [{interestKey:Dogs},{interestKey:Cats}]}");
List<String> list = new ArrayList<String>();
JSONArray array = obj.getJSONArray("interests");
for(int i = 0 ; i < array.length() ; i++){
list.add(array.getJSONObject(i).getString("interestKey"));
}
回答by Mark Bramnik
I think you can use one of the following approaches:
我认为您可以使用以下方法之一:
The first one: define a domain object and use frameworks to "deserialize" string into this object. GSON/Hymanson should work for you here
第一个:定义一个域对象并使用框架将字符串“反序列化”到这个对象中。GSON/Hymanson 应该适合你
Example:
例子:
public class MyObject {
String [] test;
String name; // the second name in example is wrong json
}
Now use GSON like this:
现在像这样使用 GSON:
MyObject obj = new Gson().fromJson(yourStringComesHere, MyObject.class);
obj.name
Of course you should provide getters/private access to the data fields in the MyObject class, but this pretty much explains the idea.
当然,您应该为 MyObject 类中的数据字段提供 getter/private 访问,但这几乎解释了这个想法。
Alternatively you can keep working with JSONObject, it should have getters, like this:
或者,您可以继续使用 JSONObject,它应该有 getter,如下所示:
JSONObject jsonObject = getObjectSomehow(); jsonObject.get("name");
JSONObject jsonObject = getObjectSomehow(); jsonObject.get("name");
Hope this helps
希望这可以帮助
回答by Ravi Thapliyal
If you're using the org.json.Java parser, here's how to do it:
如果您使用的是org.json。Java解析器,这是如何做到的:
String jsonData = "{\"test\":[\"test\",\"test1\"],\"name\":\"xxxxx\",\"id\":\"1234\"}";
JSONObject jsonRoot = new JSONObject(jsonData);
List<String> list = new ArrayList<String>();
JSONArray jsonList = jsonRoot.getJSONArray("test");
for (int i = 0; i < jsonList.length(); i++) {
list.add(jsonList.getString(i));
}
String name = jsonRoot.getString("name");
Integer id = Integer.valueOf(jsonRoot.getInt("id"));
回答by GrIsHu
create an instance of JSONObject class and using for loop through each json item and finally storing each json data in variable.
创建 JSONObject 类的实例并使用 for 循环遍历每个 json 项,最后将每个 json 数据存储在变量中。
JSONObject jsonRoot = new JSONObject(jsonData); List<String> list = new ArrayList<String>(); JSONArray jsonList = jsonRoot.getJSONArray("test"); for (int i = 0; i < jsonList.length(); i++) { list.add(jsonList.getString(i)); } String str_name = jsonRoot.getString("name"); Integer str_id = Integer.valueOf(jsonRoot.getInt("id"));
JSONObject jsonRoot = new JSONObject(jsonData); List<String> list = new ArrayList<String>(); JSONArray jsonList = jsonRoot.getJSONArray("test"); for (int i = 0; i < jsonList.length(); i++) { list.add(jsonList.getString(i)); } String str_name = jsonRoot.getString("name"); Integer str_id = Integer.valueOf(jsonRoot.getInt("id"));
Hope this will help you.
希望这会帮助你。
回答by Aravind Cheekkallur
Object s=devid.get("test");
ArrayList<String> d=new ArrayList<>();
d=(ArrayList<String>) s;
for(Object i: d)
{
System.out.println(i);
}
System.out.println(obj.get("name"));
System.out.println(obj.get("id"));
*output*
test
test1
xxxxx
1234
回答by Salahin Rocky
you can use the following code to parse json:
您可以使用以下代码来解析 json:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Map<String,Object> map = new HashMap<String,Object>();
ArrayList<String> testList = new ArrayList<String>();
String jsonString;
Gson gson = new Gson();
try {
jsonString = "{"test":["test","test1"],"name":"xxxxx","id":"1234"}";
map = (Map<String,Object>) gson.fromJson(jsonString, map.getClass());
testList = (ArrayList<String>) map.get("test");
}
catch (Exception e) {
}