I have to read from JSON file to two arraylist which one contains question and second contains answer but I don't know how to write that file. It's going to be a simple quiz. Can anyone give me an example
Asked
Active
Viewed 63 times
-3
-
Take a look here: http://stackoverflow.com/q/2591098/5810051. Possibile duplicate – FMiscia Oct 26 '16 at 11:59
-
Nothing at all because I have no clue how should it look like. It's just a programm for my classes and my instructor haven't explained anything – Loszek Oct 26 '16 at 11:59
2 Answers
0
Maybe something like this?
{
"questions":
[
"What is the capital of France?",
"What is the answer to everything?"
],
"answers":
[
"Paris",
"forty-two"
]
}
An alternative way would bundling question-answer-pairs:
[
{
"q": "What is the capital of France?",
"a": "Paris"
},
{
"q": "What is the answer to everything?",
"a": "42"
}
]
It depends on your application which way is more convenient and natural.

Landei
- 54,104
- 13
- 100
- 195
-
Got your point so if I would have next question and aswear just need to add after comma? – Loszek Oct 26 '16 at 12:03
-
0
{
"questions":["Question1","Question2","Question3"],
"answers":["Answer1","Answer2","Answer3"]
}
i suggest you to create a simple class for generate the Json from an Object
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JavaToJson
{
public static void main(String[] args) throws IOException
{
ArrayList<String> test = new ArrayList<String>();
test.add("Question1");
test.add("Question2");
try
{
String jsonStr = new ObjectMapper().writeValueAsString(test);
System.out.println("test: "+jsonStr);
}
catch (JsonProcessingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
so the variable "jsonStr" will be the json you are looking for.

RudiDudi
- 455
- 1
- 7
- 18