I am now using java method com.mongodb.util.JSON.parse(param) to convert my json string to a DBObject.I find that, in most cases , for example , when the key in the json string is simple type like string or int ,it can be used by both JSON.parse() method and terminal.
For example:
json string:{'times':8}
Both JSON.parse({'times':8}) and db.collection.find({'times':8}) in terminal can work correctly.
But when I do a query on ISODate or _id,things become different:
json string 1 :{'createDate':ISODate('2013-10-21T06:39:16.692Z')}
json strign 2: {'createDate':{'$date':'2013-10-21T06:39:16.692Z'}}
json string 1 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 2 can be parsed by JSON.parse() method but cannot be used in terminal.
The same thing happened in _id.
json string 3:{'_id':ObjectId('1231daf213432414321431')}
json string 4:{'_id':{'$oid':'fadf234234sdfadfasdfa12'}}
json string 3 can work correctly in terminal but cannot be parsed by JSON.parse() method.
On the contrary , json string 4 can be parsed by JSON.parse() method but cannot be used in terminal.
I don't know the reason of these differences.
The constructors you are using in the Mongo Shell are JavaScript code that is obviously not part of strict JSON that you want to parse in your Java application. The point to be criticized here is rather that the shell does not support something like '$date' (as I have just tested myself as well).
This is for example discussed here.
Related
I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html
I'm upgrading some legacy Android / Java code that pulls in data from a server. The legacy code uses HttpURLConnection and pulls in a inputStream and converts it to a GZipInputStream. The result is then converted to a JSONObject. Then the data is parsed manually. This all works fine, meaning valid json is returned as it starts with '{'
I'm trying to updgrade this code using Retrofit2 and the GsonConverterFactory so I can auto map the json to Classes.
I'm getting an error: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
This tells me that the data is coming in as a string and not a JSON object.
NOTE: I'm sending the same headers and query parameters for both my legacy code and new Retrofit implementation --- but different result -- can't figure out why.
The issue was that the server was expecting all of my query parameters to be in one field and the field named "Object" and in a json format i.e: Object={"field1":"fieldOneValue","field2":"fieldTwoValue"} AND had to be Url Uncoded Format:
#FormUrlEncoded
#POST("testing/myapi.api")
Observable<MyClass> getMyApiData(#Url String url, #Field("Object") String object);
The webservice: http://services.groupkt.com/country/search?text=lands has 16 records returned from a GET request. In the records, there is a parameter called 'name'. So, there are 16 names and each name has a unique value (country). My intention is to list all the 16 values of the 'name'parameter using java and the RESTassured library. I tried this:
Response response = RestAssured.get("http://services.groupkt.com/country/search?text=lands").andReturn();
String json = response.getBody().asString();
JsonPath jp = new JsonPath(json);
List<String> ls = from(response).getList("RestResponse.result.name");// The 'from' text displays an error
An error was seen on the 'from' text and it says: The method from(String) in the type RestTest is not applicable for the arguments (Response). I am not sure how to rectify this. Is there a simple way to create a list of all the values of the 'name' parameter?
Try replacing from(response) to from(jp). That should take care of the error you are getting.
The Response class (which is what the get() method returns) also supports jsonPath, so you could also refactor it to something like this:
List names= get("http://services.groupkt.com/country/search?text=lands").jsonPath().getList("RestResponse.result.name");
Am using http://jsoniter.com/ java lib to parse json , it seems with this lib , it only possible to iterate over through the JSON and did not provide any api to get value for specific key like we have in org.json like below
jsonObject.get("some_key")
so do we have such type of getter methods in http://jsoniter.com/ also , can any one please help me in that .
This is probably already too late, but you can do it like this:
String jsonString = "{'a':1,'b':'text'}".replaceAll("'", "\"");
Any jsonObject = JsonIterator.deserialize(jsonString);
long number = jsonObject.get("a").toLong();
String text = jsonObject.get("b").toString();
Is there any out of box method in java (groovy) that converts the string like
String s = "[a:12,b:[a:b,c:d]]";
to a Map object with key value pairs.
Update: It is somehow similar to the question asked as Groovy: isn't there a stringToMap out of the box?(Groovy: isn't there a stringToMap out of the box?). The difference is here the keys are of string type which makes easier to parse, since i have retrieved the mentioned map by doing .toString() i am unable to parse by the answered methods because my string actually contains date strings which i need it back to date objects. So it was difficult to parse the whole string.
You can convert this string to JSON-like format, and simple parse it:
String s = "[a:12,b:[a:b,c:d]]";
def result = new JsonSlurper().setType(JsonParserType.LAX).parseText(s.replaceAll('\\[', '{').replaceAll('\\]', '}'))
I have a way that may work depending on your actual data. The problem with your sample data is that you supply unquoted strings b and d as part of the structure:
String s = "[a:12,b:[a:b,c:d]]";
If what you had was actually
String s = "[a:12,b:[a:'b',c:'d']]";
or
String s = "[a:12,b:[a:1,c:2]]";
Then you could just do this:
def map=Eval.me(s)
but as is, eval tries to resolve b and d as variables which aren't defined in the scope.