I am having a JSON Object as below:
{
"winame": "123",
"val": "[
{
"gurName": "sds",
"gurType": "",
"crNo": "",
"crissueDate": "",
"dob": "",
"gender": "",
"address": "",
"maritialStatus": "",
"cache": ""
}
]"
}
In which first key is value and second key contains the values of some Java Object type. How to parse the value of 'winame' and 'val' into that java Object.
You can parse JSON with any number of keys.
JSONObject obj = new JSONObject("xyz");
String winame= obj.getJSONObject("winame");
JSONArray arr = obj.getJSONArray("val");
for (int i = 0; i < arr.length(); i++)
{
String gurName = arr.getJSONObject(i).getString("gurName");
String gurType = arr.getJSONObject(i).getString("gurType");
String crNo = arr.getJSONObject(i).getString("crNo");
String crissueDate = arr.getJSONObject(i).getString("crissueDate");
String dob = arr.getJSONObject(i).getString("dob");
String gender = arr.getJSONObject(i).getString("gender");
String address = arr.getJSONObject(i).getString("address");
String maritialStatus = arr.getJSONObject(i).getString("maritialStatus");
String cache = arr.getJSONObject(i).getString("cache");
}
Related
I am new to Vb.net. Having a JSON string as given below, I tries to deserialize the JSON into an Object using JsonConvert.DeserializeObject().
I am trying to loop values inside the Content object in the given JSON to fetch its values.
I tried using a for loop, but I'm not able to get the exact values.
Dim result As String = {
"status": "0001",
"Result": {
"IsError": "0",
"Data": {
"Type": "a",
"Header": [
"v1",
"v2",
"v3",
"v4",
"v5"
],
"Content": [
[
"001",
"Raj",
"1",
"N",
""
],
[
"002",
"Vignesh",
"1",
"N",
""
],
[
"778",
"Ramesh",
"1",
"N",
""
],
[
"792",
"Suresh",
"1",
"N",
""
],
[
"703",
"Karthick",
"1",
"N",
""
],
[
"1247",
"Ram",
"1",
"N",
""
]
]
}
}
}
Dim jsonResult2 = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(result)
If you just want to deserialize the Content array, you can parse the JSON with JToken.Parse(), then deserialize to a List(Of String()) that section only.
Dim jsonResult = JToken.Parse(result)
Dim content = JsonConvert.DeserializeObject(Of List(Of String()))(
jsonResult("Result")("Data")("Content").ToString()
)
Or, you could use a class Model to deserialize the whole JSON, then access each object as a standard .Net Property value.
Public Class StatusResultsRoot
<JsonProperty("status")>
Public Property Status As String
Public Property Result As Result
End Class
Public Partial Class Result
Public Property IsError As String
Public Property Data As Data
End Class
Public Partial Class Data
<JsonProperty("Type")>
Public Property DataType As String
Public Property Header As List(Of String)
Public Property Content As List(Of String())
End Class
'[...]
Dim statusResult = JsonConvert.DeserializeObject(Of StatusResultsRoot)(result)
The Content List is then
Dim content As List(Of String()) = statusResult.Result.Data.Content
' Loop the List of String(), print the combined array of strings
For Each stringArray As String() In content
Console.WriteLine(String.Join(", ", stringArray))
Next
In case you actually want to embed a JSON as a string, you can use an XML element literal, defining an XElement by enclosing the JSON with <node> ... </node> markers.
You can then paste in the JSON as is (no need to double the double-quotes).
The JSON is then the string representation of the first node of the XElement: i.e., [XElement].FirstNode.ToString(). For example:
Dim xResult = <json> {
"status": "0001",
"Result": {
"IsError": "0",
"Data": {
' [... other content ...]
}
}
} </json>
Dim json As String = xResult.FirstNode.ToString()
You cannot do this in C#.
A string in VB must begin and end with a ".
The inner double quotes should be replaced with "".
So a proper string(when assigning it in code) would look like:
Dim result as string = "{
""status"": ""0001"",
...
}
"
I have the following setup:
A Rest endpoint to accept JSON POST request:
I am parsing the request and sending it as a String over Kafka.
But getting parsing errors
A Rest endpoint to accept JSON POST request:
[ { "Name": "Jack", "Id": "314", "Gender": "M" } , { "Name": "John", "Id": "451", "Gender": "M" }, { "Name": "Rita", "Id": "501", "Gender": "F" } ]
I am parsing the request as follows
#RequestMapping(method = RequestMethod.POST, value = "/record")
#ResponseBody
public String process(#RequestBody Map<String, Object>[] payload) throws
Exception {
String str = Arrays.toString(payload)
KafkaProd.toTopic(str);
System.out.println("Payload: " +str);
return "Record Processed";
}
str = Arrays.toString(payload) is changing it into the following format
[ { Name = Jack , Id = 314, Gender = M } , { Name = John, Id = 451,
Gender = M }, { Name = Rita, Id = 501, Gender = F } ]
When I'm trying to parse this string back into json array using json-s
imple :
JSONArray jsonArray = new JSONArray(record.value());
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println("Json Objects : "
+jsonArray.getJSONObject(i).toString());
}
I am getting JSON Parsing error, since the record.value() is not a valid json array
Option 1. How do I convert this to a valid json array?
Option 2. How do I send the json array in a proper format through kafka?
Which of these options do I use?
Instead of String str = Arrays.toString(payload) use a Jackson ObjectMapper to convert the map to a String.
I want to do an API call using HttpUrlConnection for POST as the JSON content looks like this
To POST I need to pass key as id and value as 1234.
{
"message": "Success",
"constituency": [{
"id": "1",
"constituency_name": "abc"
}, {
"id": "2",
"constituency_name": "def"
}]
}
I need to fetch this array of strings. How do I do this?
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
OK i know how to parse using newtonsoft.
but I don't know how can i get every value of key inside parsed string
this is the json encoded string
{"result":[{"orderid":"94","imei":"clipper"},{"orderid":"93","item":"shoes"},{"orderid":"92","item":"bag"},{"orderid":"91","item":"shirt"}]}
Dim xreadingJson = Newtonsoft.Json.Linq.JObject.Parse(htmlcode)
Dim resultorder As String = xreadingJson.Item("result").ToString
then the result order is
[
{
"orderid": "94",
"item": "clipper"
},
{
"orderid": "93",
"item": "shoes"
},
{
"orderid": "92",
"item": "shoes"
},
{
"orderid": "91",
"item": "bag"
}
]
On looping how can I get the value of orderid and item.
thank you
Update:
I resolved it using this code
Dim o As JObject = JObject.Parse(htmlcode)
Dim results As List(Of JToken) = o.Children().ToList
For Each item As JProperty In results
item.CreateReader()
'MsgBox(item.Value)
If item.Value.Type = JTokenType.Array Then
For Each subitem As JObject In item.Values
MsgBox(subitem("orderid"))
MsgBox(subitem("item"))
Next
End If
Next
I believe Newtonsoft's JObject has a JObject.GetValue("property_name") method
I have a very complex JSON returning from an API. I need to pass only the "first level" to the client side, without all the nested objects contained in it.
For example:
{
"name": "David",
"age": 5,
"school": {
"name": "Highschool",
"location": "AZ"
}
}
I'd like to pass to the client side only name & age, not "school".
Is there a simple way to do that?
You could parse the JSON into a JObject then copy all the "simple" properties (i.e. those that are not objects and arrays) to a new JObject. Then get the new JSON from the copy.
For example:
string json = #"
{
""name"": ""David"",
""age"": 5,
""school"": {
""name"": ""Highschool"",
""location"": ""AZ""
}
}";
JObject origObj = JObject.Parse(json);
JObject copyObj = new JObject();
foreach (JProperty prop in origObj.Properties())
{
if (prop.Value.Type != JTokenType.Object &&
prop.Value.Type != JTokenType.Array)
{
copyObj.Add(prop.Name, prop.Value);
}
}
json = copyObj.ToString();
Console.WriteLine(json);
The above will output the following:
{
"name": "David",
"age": 5
}