0

I'm doing a webservice in java and in the post method i need to receive a json from the front-end

And my doubt is that i can read dynamically json

Lets pretend that sometimes i receive this

'{ "bit1":"000", "bit2":"1111", "bit50":"010101" }';

and in the next day i receive something like that:

'{ "bit3":"101", "bit7":"00010", "bit30":"1010111", "bit40":"1010001" }';

I dont know what the webservice will receive, the length, the name of the params, i only know that is a json well formed. There are any way to read it?

Thanks for your time

1
  • You'd start by picking a library that can parse the JSON … and then the specifics of how you find out what properties are in it would probably depend on exactly what type of object that library presented the parsed data as. Commented May 8, 2017 at 9:33

2 Answers 2

1
@Post
@Consume("application/json")
@Produce("application/json")
public String recieveMyJson(JSONObject myJSON)
{
// here myJSON will recieve any valid json structure regardless to the length
String []string=myJSON.toString().split(","); // should do what do you need
for(String myKey:string)
{
System.out.println(myKey)
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

hum ok, different approach than i initially thought , but it works just fine. Thank you
1

Use json-1.5.jar (or any other version of it), it provides options to read all the key_list as string array,

String[] keys = JSONObject.getNames(YOURjsonOBJECT);

after reading keys into String array, iterate over keys to read their values from YOURjsonObject one at a time ( or you may use string array as parallelStream for parallel and faster reading).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.