I have a string created from a JSON object as follows
{
"id" : "112233",
"someElement" : [ {
"map" : {
"123" : [ {..}]
},
"map" : {
"124" :[ {..}]
}
}]
}
I need to convert the above string to the following format.
{
"id" : "112233",
"someElement" : [ {
"123" : {
"element" : [ {..}]
},
"124" : {
"element" :[ {..}]
}
}]
}
I tried to do string substitution as when the substring "map" is found in the string, replace with the ID just beneath it.
String a = jsonString.substring(jsonString.indexOf("map")+16, jsonString.indexOf("map")+19);
String b = jsonString.replace("map", a);
This pattern works for the first occurrence of "map" string. But the same ID value replaces the second "map" string. How do I replace the subsequent occurrences of "map" string with their respective IDs.
Also, is there any better way to do this? Appreciate any feedback.Thanks!
JSON is not a regular language, so trying to make this kind of change with a regular expression will be fragile; syntactically insignificant variations in the input will easily confuse your regular-expression–based solution.
Because this example violates the JSON recommendation for keeping object member names unique, many JSON parsers will have difficulty parsing it, raising an exception or ignoring some members. However, there might be parsers out there that handle it. If not, it's very easy write your own parser for JSON that will handle this input robustly. Then your code won't break when the whitespace changes.
Related
I have a JSON Request with the body of a request containing three parameters. I need to check whether the 'value' for the 'key' is empty or not in the body of the JSON request. I'm trying this using java code. How can I check this condition in my code?
This is my JSON Request:
{
"Details": [
{
"EmpId" : "123456",
"DeptId" : "12345678",
"Name" : "abc"
}
]
}
Please Note: I have two parameters that have integer values and one that has a String value.
As you have a response in JSONObject jsonObject, you have to check for the key you are looking for, like this :
if(!jsonObject.isNull("EmpId")) {
}
jsonObject.isNull("key") is the method used for checking whether the key is present or not. If the key is not present, this method returns true.
One thing to remember, this method only check whether the key is present in json object or not. If present, you can alternatively check for the value is empty or has some values.
Then check for the value of that particular key like this :
String employeeId = jsonObject.getString("EmpId");.
So you have to traverse like this :
if(!jsonObject.isNull("EmpId")) {
String employeeId = jsonObject.getString("EmpId");
}
Hope it will help you.
I'm having trouble creating a pojo from this json:
"versionAudio": {
"Simulcast": [
"English",
"Japanese"
]
},
{
"Uncut": [
"English",
"Japanese"
]
}
The versionAudio object can contain any number of String arrays and the name can be anything. I know I can't create a class for versionAudio. It will have to be an JSONObject that I will pull data out of. I'm not sure how begin. I couldn't find any SO questions that relate to this. Most cases, the name of the field is known.
Your versionAudio class should to have one field Map<String, String[]>, try use it
I am trying to create a json schema for a corresponding Java enum of variable length values. Information follows :
JSON tried :
"Info": {
"type": "string",
"enum": [
"TITLE",
"ADDRESS",
"NAME";
]
}
But this wouldn't provide the desired output instead the converted java class I am getting is
Current Output :
public static enum Info {
TITLE("TITLE"),
ADDRESS("ADDRESS"),
NAME("NAME");
}
Required Java Output :
public enum Info {
TITLE(45),
ADDRESS(100),
NAME(45);
private Integer maxLength;
Info(Integer maxLength) {
this.maxLength = maxLength;
}
public Integer getMaxLength() {
return maxLength;
}
}
Unable to figure out a way to solve this. Would appreciate any help.
As far as I can tell this is currently not possible. If you look at the source code, then enums are generated by org.jsonschema2pojo.rules.enumRule. jsonschema2pojo provides a constructor to each enum which for your use case would need to take a single argument of type Integer. The constructor is generated in line 199 by the following code
JVar valueParam = constructor.param(String.class, VALUE_FIELD_NAME);
That is, the constructor is hard coded to always take a single argument of type String.
The best you can probably do is to use the javaEnumNames property which allows you to specify the names and values of your enums (with the restriction that the value is always a string).
{
"javaEnumNames" : [
"TITLE",
"ADDRESS",
"NAME"
],
"enum" : [ 45, 100, 45 ]
}
This JSON snippet produces
...
TITLE("45"),
ADDRESS("100"),
NAME("45");
...
I would like to know, whats the right structure for a list of objects in JSON.
We are using JAXB to convert the POJO's to JSON.
Here is the choices, Please direct me what is right.
foos: [
foo:{..},
foo:{..}
]
or
foos : [
{...},
{...}
]
If the first structure is right, what is the JAXB annotation I should use to get the structure right.
The second is almost correct:
{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
The first example from your question,
foos: [
foo: { ... },
foo: { ... }
]
is in invalid syntax. You cannot have object properties inside a plain array.
The second example from your question,
foos: [
{ ... },
{ ... }
]
is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.
Following is the correct one when you want to obey strict JSON:
"foos": [
{ ... },
{ ... }
]
This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.
As others mentioned, Justin's answer was close, but not quite right. I tested this using Visual Studio's "Paste JSON as C# Classes"
{
"foos" : [
{
"prop1":"value1",
"prop2":"value2"
},
{
"prop1":"value3",
"prop2":"value4"
}
]
}
I use jsonformatter page to test the json validator.
try to use jsonwhatever
example:
list_a = func_generator_of_objects()
json_string = jsonwhatever('foos',list_a)
I am trying to output some Java objects as JSON, they have List properties which I want to be formatted as { "People" : [ { "Name" : "Bob" } , { "Name" : "Jim" } ] }
However, I cannot figure out how to do this with XStream. It always outputs as { "Person" : { "Name" : "Bob" }, "Person" : { "Name" : "Bob" }
Is there a way to fix this? I've put together some sample code with a unit test in github if you need something more concrete to play with: http://gist.github.com/371358
Thanks!
I assume that the problem is with #XStreamImplicit which is declared on top of the List, remove that and try. It should work fine ;)
/jay