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

How to access value of inval when the f1.png value is not fixed. i.e. the name of file can be anything, its not known so how can I access value for inval field for various files in this JSON using Java?

2
  • you sure you mean Java ? Commented Jun 12, 2013 at 10:16
  • 1
    There are JSON libraries to do that. Commented Jun 12, 2013 at 10:20

5 Answers 5

6

Please try below code,

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();
        }

Hope it solves your issue

Sign up to request clarification or add additional context in comments.

Comments

1

Using Jackson and JsonNode, you'd do:

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");

to access the inner object.

Then to walk the filesNode object you'd do:

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")
}

If you can use this project this becomes more simple:

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

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

Comments

1

You can use JsonPath library to access child elements. https://github.com/json-path/JsonPath

It can be as simple as

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

With some modifications.

Comments

1

You can use ObjectMapper.

First create a class Image.

import lombok.Data;

@Data
public class Image {

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

}

and convert to Map

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>>>() {});

Thanks.

Comments

0

for nested array type,

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

} below code will help.

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"));
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.