Jean Tennie
Jean Tennie

Reputation: 253

fill array from json file without knowing the keys

I have the following Json file and I want to create list of array ,i don't know the keys name (like person ,lastname etc ) it can be many entities like employes users etc but the structure of the file must be exactly the same ,how can i do that ?

the file is

{
    "Person": [
        {
            "name": "Peter",
            "lastname": "ta",
            "age": 43,
            "sex": "male"
        },
        {
            "name": "Zara",
            "lastname": "treg",
            "age": 25,
            "sex": "female"
        }
    ]
}

what I need is to create list of array like this

person ,name,peter ,lastname,ta,age,43,sex,male
person ,name,zara ,lastname,treg,age,23,sex,female

....

I started with the following code to get the file but since i dont know the name of the keys I dont know how to proceed.

JSONObject jsonObject= (JSONObject) parser.parse(new FileReader("C:\\General\\jsonperson.txt"));

Upvotes: 1

Views: 1160

Answers (3)

HarshaKA
HarshaKA

Reputation: 165

You can use

String[] keyNames= JSONObject.getNames(jsonObject);

to get the names of the keys. Javadoc

Using this, you can get the values using getJSONObject

Looping these you can construct the array you are looking for.

Upvotes: 2

1218985
1218985

Reputation: 8022

You can also try the below code:

public void performExecute() throws IOException {        
        JsonFactory jsonFactory = new JsonFactory(); 
        ObjectMapper objectMapper = new ObjectMapper(jsonFactory); 
        File file = new File("json.txt"); 
        TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() {}; 
        HashMap<String, Object> jsonMap = objectMapper.readValue(file, typeReference); 
        System.out.println("JSON DATA: " + jsonMap); 
    } 

Make sure the Jackson library is in your class path & you imports the following classes while using the code:

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

Upvotes: 1

jonvuri
jonvuri

Reputation: 5930

Check Example 4 on this page: https://code.google.com/p/json-simple/wiki/DecodingExamples

Specifically, this part:

Map json = (Map)parser.parse(jsonText, containerFactory);
Iterator iter = json.entrySet().iterator();
System.out.println("==iterate result==");
while(iter.hasNext()){
  Map.Entry entry = (Map.Entry)iter.next();
  System.out.println(entry.getKey() + "=>" + entry.getValue());
}

System.out.println("==toJSONString()==");
System.out.println(JSONValue.toJSONString(json));

That's how you might iterate over the entries of a JSONObject. By the way, with this library, if it's the one I think you're using, a JSONObject is just a java.util.Map, and you can use all of its methods - that's why it works for this example to cast the parse result to a Map.

All of the JSON <-> Java object mappings for this lib: https://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities

Upvotes: 1

Related Questions