i have a json string like
{"id":0,"version":0,"subject":"[2sravya] data","description":"[10nd data] description [AWS Account:12234]","createdDate":null,"visibleToCustomer":false,"externalEMailId":null}
After it is converted to JSONObject using JSONSerializer.toJSON() the JSONObject contains value like
{
"id": 0,
"version": 0,
"subject": "[2sravya] fdaya",
"description": [
"10nd data"
]
}
data present in description is a string data. i want the same to be present in formatted JSONObject.
Related
i have a api response data in this format
{
"channel": [
{
"link": "https://www.foxnews.com",
"title": "FOX News : World ",
"url": "https://global.fncstatic.com/static/orion/styles/img/fox-news/logos/fox-news-desktop.png"
}
],
"opstatus": 0,
"httpStatusCode": 200
}
and want to print the link and title values. how can i do that?
try this
import json
response =.. your json string;
jsonObj = json.loads(response)
print(jsonObj["channel"][0]["link"])
print(jsonObj["channel"][0]["title"])
//or if you have several objects in channel array
for each in jsonObj['channel']:
print each['link']
I pass a string to my view in this code snippet:
List<List<Book>> data = getData();
string jsonData = JsonSerializer.Serialize<List<List<Book>>>(data);
ViewData["Orders"] = jsonData;
getData() returns a list of list of a class, so jsonData looks something like this after the JSON Serialize:
[
[ "title": "Some title", "price": 12.00 ],
[ "title": "Some other title", "price": 23.00 ]
]
Now, how in the world do I parse this in my view? I can do something like this
#{
ViewData["Title"] = "Books";
}
<p>#ViewData["Orders"]</p>
And print out the content, but there do not seem like any way to parse the string to a JSON object so I can present it. I have tried to instead pass the data variable to the ViewData but then I can not parse it since it can not find the Book class, so I am so confused...
I'm having a heck of a time figuring out how to unmarshal a large unstructured json response into a usable object.
Here is a sample response (trimmed to show the part I'm having trouble with)
This has been greatly trimmed as this is a very large json response. I left the struct tags off below as well for simplicity.
{
"responseStatus": "ok",
"responseHeader": {
"status": "ok",
"requestId": "blah"
},
"responseData": {
"records": [
{
"name": "blah",
"site": {
"id": 1,
"name": "west"
},
"somevar1": "someval",
"somevar2": {
"x": 2,
"y": 1
},
"entries": [
{
"model": "100",
},
{
"model": "200",
}
]
},
]
}
So records is a large list of "objects". I need to convert these to a type I defined. The "entries" list also needs to be converted to its object type.
type Record struct {
Name string
Site map[string]string
Somevar1 string
Somevar2 map[string]string
Entries []Entry
}
type Entry struct {
Model string
}
Here I can get the responseData into an object I can iterate over:
results := data["responseData"].(map[string]interface{})
devices := results["records"].([]interface{})
for _, device := range devices {
fmt.Fprintf(os.Stdout, "%T\n", device)
fmt.Fprintf(os.Stdout, "%+v\n", device)
}
Here is a sample output from 1 iteration:
map[string]interface {}
map[name:foo site:map[id:2 name:somewhere somevar1: blah somevar2:map[x:1 y:2] entries:[map[model:100] map[model:200]
This is where I'm stuck. I need to take this line above and get convert it into my type Record while also converting the Entries to []Entry.
Change the Site and Somevar2 fields to map[string]interface{} or to a proper struct, because their corresponding json contains ints, so when you use only map[string]string it will fail.
Other than that your code works https://play.golang.com/p/rTgaXhXD1V6
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 have the following json object that I am trying to represent with Go as a type JsonObject struct
and pass it back in its original json so that I can return the json as an api endpoint. Any advice/example?
[{
"time": 173000,
"id": "VLSuEE5m1kmIhgE7ZhHDFe",
"height": "",
"DATASTRUCTURE": {
},
"language": "en",
"size": 0,
"url": "http://www.gstatic.com/play.m3u8",
"type": "vid",
"definitionid": "h264",
"reference": "PAN-EN",
"content": "This is some content",
"revisiondate": "2017-11-29T00:00:00",
"data": {
},
"id": "BBBB3424-153E-49DE-4786-013B6611BBBB",
"thumbs": {
"32": "https://www.gstatic.com/images?q=tbn:ANd9GcRj",
"64": "https://www.gstatic.com/images?q=tbn:DPd3GcRj"
},
"title": "Cafeteria",
"hash": "BBBB5d39bea20edf76c94133be61BBBB"
}]
You can use https://mholt.github.io/json-to-go/ to generate struct for the given json schema.
For example, json given in the question can be represented like:
type AutoGenerated []struct {
Time int `json:"time"`
ID string `json:"id"`
Height string `json:"height"`
DATASTRUCTURE struct {
} `json:"DATASTRUCTURE"`
Language string `json:"language"`
Size int `json:"size"`
URL string `json:"url"`
Type string `json:"type"`
Definitionid string `json:"definitionid"`
Reference string `json:"reference"`
Content string `json:"content"`
Revisiondate string `json:"revisiondate"`
Data struct {
} `json:"data"`
Thumbs struct {
Num32 string `json:"32"`
Num64 string `json:"64"`
} `json:"thumbs"`
Title string `json:"title"`
Hash string `json:"hash"`}
Hope this helps!