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));
Related
Anybody has any idea how to parse below type of JSON object where same object named objects are present in master object. Below is an example
abc{
"abc":"123"
}
How to parse the child abc value?
You can use Json.NET it is vary easy to use. Json.NET handles JSON arrays natively, and will parse them into any type, string,int etc.
Are you currently using a JSON framework? Maybe check out Json.NET: http://www.newtonsoft.com/json.
Then you could do something like:
int onetwothree = jObject.SelectToken("abc.abc");
You can use http://www.newtonsoft.com/json to parse any json string on c#:
Your example :
var jsonString= "{\"abc\":{ \"abc\":\"123\" }}";
var jObj = JObject.Parse(jsonString);
var abcValue = jObj.SelectToken("abc.abc").Value<string>();
I am using Mule 3.6.1 and in datamapper I have a JSON object which is a string datatype and I need to get the value of a field from the JSON object.
How can I get this value from the JSON object while the object is of type String?
I cannot use the JSON transformer for this.
Thanks for any help
To convert a String of JSON and get one of its field value inside DataMapper, then you can utilize code like this (in DataMapper Script area):
jsonObject = new org.json.JSONObject(input.jsonstring);
output.jsonValue = jsonObject.getString("jsonfield");
In order to convert JSON element to a series of objects. Google GSon library is very helpful.
Example:
import com.google.gson.Gson;
Gson gson = new Gson();
Student studentTest = gson.fromJson(data, Student.class);
System.out.println("Amount: " + studentTest .getStudentName());
I'm trying to deserialize some JSON coming back from couchbase into a dynamic type.
The document is something like this so creating a POCO for this would be overkill:
{
UsersOnline: 1
}
I figured that something like this would do the trick, but it seems to deserialize into a dynamic object with the value just being the original JSON
var jsonObj = _client.GetJson<dynamic>(storageKey);
results in:
jsonObj { "online": 0 }
Is there anyway I can get the couchbase deserializer to generate the dynamic type for me?
Cheers
The default deserializer for the client uses .NET's binary serializer, so when you save or read a JSON string, it's just a string. GetJson will always just return a string. However, there are a couple of options:
You could convert JSON records to Dictionary instances:
var appJson = "{ \"UsersOnline\" : 1, \"NewestMember\" : \"zblock\" }";
var result = client.ExecuteStore(StoreMode.Set, "userCount", appJson);
var item = client.GetJson<Dictionary<string, object>>("userCount");
Console.WriteLine("There are {0} users online. The newest member is {1}.",
item["UsersOnline"], item["NewestMember"]);
Or you could use a dynamic ExpandoObject instance:
var appJson = "{ \"UsersOnline\" : 1, \"NewestMember\" : \"zblock\" }";
var result = client.ExecuteStore(StoreMode.Set, "userCount", appJson);
dynamic item = client.GetJson<ExpandoObject>("userCount");
Console.WriteLine("There are {0} users online. The newest member is {1}.",
item.UsersOnline, item.NewestMember);
In either case you're losing static type checking, which seems like it's OK for your purposes. In both cases you get access to the JSON properties without having to parse the JSON into a POCO though...
Edit: I wrote a couple of extension methods that may be useful and blogged about them at http://blog.couchbase.com/moving-no-schema-stack-c-and-dynamic-types
im using jackson to deserialize some Json. I am reading through a large json document and pulling out blocks and telling jackson to take that block and deserialize it to an object that I created (Actually several objects as there are nested arrays) in java to match the json.
The code im using to deserialize is
fooObject newFoo = mapper.readValue(newNode,fooObject.class);
The problem is there is a value in the block that is sometimes a hash such as
addWidgetStrategy={"get":2,"spend":6,"textLabel":"bikes"}
and sometimes an array
addWidgetStrategy=[{"get":1.5,"spend":3,"textLabel":"thursday"},{"get":3,"spend":5,"textLabel":"tuesday"}]
So in fooObject I need to deal with addWidgetStrategy which has its own object. If in fooObject I put
public addWidgetStrategy addWidgetStrategy;
The above works until it tried to deserialize an array
If I put
public List<addWidgetStrategy> addWidgetStrategy;
it works just for arrays and blows up when its just a single hash
How can I parse that same Json element addWidgetStrategy regardless if its an array or a single hash?
For arrays it should be:
fooObject[] newFoo = mapper.readValue(newNode,fooObject[].class);
You can read it like this:
JsonNode jsonNode = mapper.readTree(json);
if (jsonNode.isArray()) {
fooObject[] newFoo = mapper.readValue(jsonNode,fooObject[].class);
...
} else {
fooObject newFoo = mapper.readValue(jsonNode,fooObject.class);
....
}
I have an object that is created by Newtonsoft's JSON serializer. I need to get the JSON string that was used to create the object. How do I serialize the object into a simple JSON string?
Try this:
public string jsonOut()
{
// Returns JSON string.
return JsonConvert.SerializeObject(this);
}