使用java访问嵌套的JSON对象值

时间:2013-06-12 10:15:32

标签: java json

{
    "files": {
        "f1.png": {
            "intext": "A",
            "inval": 0,
            "inbinary": false
        },
        "f2.png": {
            "intext": "A",
            "inval": 0,
            "inbinary": true
        }
    }
}

如果未修复f1.png值,如何访问inval的值。即文件的名称可以是任何东西,它是未知的,那么如何使用Java访问此JSON中各种文件的inval字段的值?

5 个答案:

答案 0 :(得分:4)

请尝试以下代码,

import org.json.JSONException;
import org.json.JSONObject;
public static void main(String[] args) {
        String jsonString = "{\"files\": {\"f1.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": false}, \"f2.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": true}}}";
        try {
            JSONObject jsonObject =new JSONObject(jsonString);
            JSONObject jsonChildObject = (JSONObject)jsonObject.get("files");
            Iterator iterator  = jsonChildObject.keys();
            String key = null;
            while(iterator.hasNext()){
                key = (String)iterator.next();
                System.out.println("inval value: "+((JSONObject)jsonChildObject.get(key)).get("inval"));
            }
        }
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

希望它能解决您的问题

答案 1 :(得分:1)

您可以使用 ObjectMapper。

首先创建一个类 Image。

import lombok.Data;

@Data
public class Image {

    private String intext;
    private Integer inval;
    private Boolean inbinary;

}

并转换为地图

final ObjectMapper objectMapper = new ObjectMapper();
final String jsonString =
      "{\"files\": {\"f1.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": false}, \"f2.png\": {\"intext\": \"A\",\"inval\": 0,\"inbinary\": true}}}";
  final Map<String, Map<String, Image>> output =
      objectMapper.readValue(
          jsonString, new TypeReference<Map<String, Map<String, Image>>>() {});

谢谢。

答案 2 :(得分:0)

使用JacksonJsonNode,您可以:

private static final ObjectReader READER = new ObjectMapper()
    .getReader;

// blah

// read the node
final JsonNode node = READER.readTree(fromWhatever);

// access the inner "files" member
final JsonNode filesNode = node.get("files");

访问内部对象。

然后走你要做的filesNode对象:

final Iterator<Map.Entry<String, JsonNode>> iterator = filesNode.fields();
Map.Entry<String, JsonNode> entry;
while (iterator.hasNext()) {
    entry = iterator.next();
    // the "inval" field is entry.getValue().get("inval")
}

如果你可以使用this project,这就变得更加简单了:

// or .fromFile(), .fromReader(), others
final JsonNode node = JsonLoader.fromString(whatever);

final Map<String, JsonNode> map = JacksonUtils.nodeToMap(node.get("files"));
// walk the map

答案 3 :(得分:0)

您可以使用JsonPath库访问子元素。 https://github.com/json-path/JsonPath

可以像

一样简单
List<String> names = JsonPath.read(json, "$.files.*);

经过一些修改。

答案 4 :(得分:0)

用于嵌套数组类型,

{
"combo":    [
                {"field":"divisions"},
                {"field":"lob"}
]

}  下面的代码将有所帮助。

Iterator<?> keys = jsnObj.keys();

    while(keys.hasNext()) {
        String key = (String) keys.next();
        JSONArray list = (JSONArray) jsnObj.get(key);
        if(list==null || list.length()==0)
            return null;
        List<String> comboList = new ArrayList<String>();
        for(int i=0;i<list.length();i++){
            JSONObject jsonObject =  (JSONObject)list.get(i);
            if(jsonObject==null)
                continue;
            comboList.add((String)jsonObject.get("fields"));
        }
    }