For client requests to ektorp / couchdb I would like to pass JSON back to the client.
(Why not use couchdb direktly? Because I have to do some tweeks to the data on a Java layer inbetween.)
So is there for example a way to get JSON data from a CouchDbRepositorySupport queryView?
As far as I know, from consulting the documentation the following should do it
ViewResult result = db.queryView(query);
for (ViewResult.Row row : result) {
JsonNode docNode = row.getDocAsNode();
}
Here's another way:
InputStream is = db.queryForStream(query);
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(is);
(JsonNode and ObjectMapper are from the org.codehaus.jackson library.)
Related
I have a String (jsonData) being mapped to json via Jackson Object mapper as below to JaxB.
var completeJson = objectMapper.readValue(jsonData, Data.class);
myDto.setFirstName(completeJson.getFirstName())
myDto.setLastName(completeJson.getLastName()))
.....
.....etc..etc
I'm able to map to the above strings just fine. However, I'm having problems mapping
to a jooq JSON object. I guess I will now have to convert jsonData to jooq JSON.
How would I do this?
JSON newJson = objectMapper.(best method to use);
myDto.setJsonSource(newJson)
Or maybe I have to create some sort of wrapper?
DTO configured by jooq
public myDto setJsonSource(JSON jsonSource) {
this.jsonSource = jsonSource;
return this;
}
The org.jooq.JSON type is just a wrapper around a string, so why not just use:
JSON json = JSON.json(objectMapper.writeValueAsString(object));
I have Json response through Facebook API like this:
Now I want to parse this data so I can use within my game. For this purpose, I have written code up to this:
public void OnChallengesButtonClick ()
{
SoundManager.Instance.PlayButtonClickSound ();
FB.API ("/me/friends", HttpMethod.GET, RetrieveFacebookFriends);
}
public void RetrieveFacebookFriends (IGraphResult result)
{
if (!string.IsNullOrEmpty (result.Error)) {
Debug.Log ("ERROR: " + result.Error);
return;
}
IDictionary<string,object> friendsDict = result.ResultDictionary;
Debug.Log ("raw: " + result.RawResult);
}
So how to extract name and ID data from available Json response through Dictionary object?
Basically you should create a class to serialize/deserialize an object:
class FBResponse
FBDataResponse data;
class FBDataResponse
string name;
string id;
Take a look to my response here for more details (after "Final EDIT" part):
Converting arrays of arrays to json
It should be easy to create those two class and use them to deserialize the FB response. Just remember that your variables will need to match the same name with the json response from Facebook (for example you cannot call data data1).
After you have the object it should be easy to do what you want with it... and with the object you should be able to get the json again.
PS. Unity default json serializer does not support Dictionary that's why you need to serialize two object. If you don't want that you should try to use a different library.
I think this is all I need: JSON with Unity
friends = (List<object>)(((Dictionary<string, object>)friendsH) ["data"]);
if(friends.Count > 0) {
var friendDict = ((Dictionary<string,object>)(friends[0]));
var friend = new Dictionary<string, string>();
friend["id"] = (string)friendDict["id"];
friend["first_name"] = (string)friendDict["first_name"];
}
Before I can't able to find this post from Facebook.
How do you convert this string to JSON data in codenameone.
{result_code=0.0, data=[{id=1007747505, name=Test, dob=1986-05-16, identification=6837904, otherinformation=, status=Active, classid=11, class=Twenty, start_date=2016-05-27, end_date=2017-05-26}], message=OK}
You can use the JSONParser class as described in the developer guide using:
JSONParser p = new JSONParser();
Map<String, Object> results = p.parseJSON(new CharArrayReader("{result_code=0.0, data=[{id=1007747505, name=Test, dob=1986-05-16, identification=6837904, otherinformation=, status=Active, classid=11, class=Twenty, start_date=2016-05-27, end_date=2017-05-26}], message=OK}".toCharArray());
The trick is to place a breakpoint after that line and look at the contents of "results" to see the data structure returned by the parser.
I use solrj to submit a query to solr , that returns the result in json format.
SolrQuery query = new SolrQuery();
SolrQuery query = new SolrQuery();
query.setParam("kw", keyword);
query.setParam("lc", location);
query.setParam("wt", "json");
query.setParam(CommonParams.QT, "/lds");
QueryResponse qResponse = solrServer.query(query);
searchResultStr = qResponse.toString();
But the searchResultStr does not have a string in JSON format. Instead it has something like this:
{responseHeader={status=0,QTime=21},location={has_zipcode=true,location_param=94085}}
But if i directly hit the solr url in the browser, I get the correct JSBON format:
{"responseHeader":{"status":0,"QTime":15},"location": {"has_zipcode":true,"location_param":"94085"}}
For a JSON output you will have to query a HTTPSolrServer using curl as mentioned in the answer here. Using EmbeddedSolrServer will not help i.e. solrj.
When you use solrj to query you will have to get the SolrDocumentList from the QueryResponseobject and convert it into JSON format by iterating through each SolrDocument and entering the data into JSON the way you want.
Solr QueryResponse returns a hashmap, so use any hashmap to json converter to get the json out of it. Here I have used Gson() for that conversion.
QueryResponse response = solr.query(query);
Gson gson = new Gson();
String json = gson.toJson(response.getResults());
System.out.println(json);
Firstly, my HTTP POST through a URL accepts 4 parameters. (Param1, Param2, Param3, Param4).
Can I pass the parameters from the database?
Once the URL is entered, the information returned will be in text format using JSON
format.
The JSON will return either {"Status" : "Yes"} or {"Status" : "No"}
How shall I do this in servlets? doPost()
Just set the proper content type and encoding and write the JSON string to the response accordingly.
String json = "{\"status\": \"Yes\"}";
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of composing the JSON yourself, you may consider using an existing JSON library to ease the job of JSON (de)serializing in Java. For example Google Gson.
Map<String, String> result = new HashMap<String, String>();
result.put("status", "Yes");
// ... (put more if necessary)
String json = new Gson().toJson(result);
// ... (just write to response as above)
Jackson is another option for JSON object marshalling.