How to extract image link from JSON in Flutter - Regex - json

I am a beginner in Flutter. I was making a note-taking app, which contains flutter_quill. When I save the data into JSON, I got this data.
[{"insert":"Ttttttt\n"},{"insert":{"image":"/data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg"}},{"insert":"\n\n"}]
I just need the .jpg file data part - /data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg, How to get that? Using Regex?

Something like this?
void main() {
var l = <Map<String, dynamic>>[
{"insert": "Ttttttt\n"},
{"insert": { "image":"/data/user/0/com.example.notes/app_flutter/image_picker49200670200148407.jpg"}
},
{"insert": "\n\n"}
];
var imageElement = l.firstWhere((element) =>
element["insert"] is Map && element["insert"].containsKey("image"));
var image;
if (imageElement!=null) image=imageElement["insert"]["image"];
print (image);
}

loop all insert then check, have key "image" so get that value

Related

typescript - load json from url and get access to array of json objects

I just can't find a working solution and implement in my format.
There is a JSON file which is returned to me by URL. Its format is:
{"success":true,
"data":[
{
"loadTimestamp":"2022-07-20T15:12:35.097Z",
"seqNum":"9480969",
"price":"45.7",
"quantity":"0.2",
"makerClientOrderId":"1658329838469",
"takerClientOrderId":"1658329934701"
},
{
"loadTimestamp":"2022-07-20T14:49:11.446Z",
"seqNum":"9480410",
"price":"46",
"quantity":"0.1",
"makerClientOrderId":"1658328403394",
"takerClientOrderId":"0"
}]
}
Due to the fact that it is returned via the URL, it is not possible to directly use the object, for example:
myobj['data']['price']
I have either a string of data that I can convert using JSON.parse() or an object right away.
But for some reason I can't use it directly.
As far as I understand, this is a JSON file inside which is an array of JSON data.
My goal is to display all the data from the array, while taking for example 2 values: price, quantity
How can I access the values that I want to get?
Ok I find, what I was looking for.
I return result not in json, but in text response.text()
After I did this, I create a new constant called res and put in it JSON.parse(data)
const url = 'https://myurl.com/'+pub_key
const response = await fetch(url)
let data = ''
if (response.ok) { data = await response.text() } else { console.log("Error HTTP: " + response.status) }
const res = JSON.parse(data)
After all this manipulations I can use my data with 2 ways:
console.log(res["data"][0]["price"])
console.log(res.data[0].price)
Or I can make a cocktail from it, by using my favorite blender :)))
if(res.success==true){
for(let item in res.data){
console.log(res.data[item].price,res.data[item].quantity)
}
}

How to update the local json field in flutter

I am new to flutter and dart I read a lot of web article and documentation but didn't able to figure out how to update JSON field I have a local JSON file like
{
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}]}
I want to update the filed favorite to true in JSON file Is there any way to do this
You Can parse the JSON like this
Map<String, dynamic> data = jsonDecode(jsonString);
And if you want to make each favorite true then You can use looping like this
(data["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
if you want to set a single object value true then you need to pass the position like this
(data["quotes"] as List<dynamic>)[position]['favorite'] = true;
Addition
Here is the link to encode and decode the JSON
You can use a forEach loop on the quotes list in the JSON and set favourite to true inside it. I'm assuming you have a parsed your String data into JSON using jsonDecode or json.decode.
If not, here's a link on how to parse JSON data in Dart.
Consider the below code -
Map<String, dynamic> jsonData = {
"category": "Happiness",
"quotes":[
{
"quote":"I hope you will find a reason to smile",
"favorite":false
},
{
"quote":"Sometimes your joy is the source of your smile, but sometimes your smile can be the source of your joy.",
"favorite":false
}
]};
(jsonData["quotes"] as List<dynamic>).forEach((item) {
item["favorite"] = true;
});
You can also alternatively use shorthand syntax for the forEach loop function like below -
(jsonData["quotes"] as List<dynamic>).forEach((item) => item["favorite"] = true);
first take this json array to any variable and then understand the hierarchy .
e.g "jsonArr" is the array that hold that data then ..
jsonArr['quotes'][0]['favorite'] = true;
or you can put this in a for loop and traverse this loop to the length of the json array.
You can use the following method. The field favorite is changed to true when the button is pressed.
FlatButton(
child: Text('Add Favourite'),
onPressed: (){
myJson['quotes'][myIndex]['favorite'] = true;
})

How to format a json string and show result in table?

I am using CakePhp _serialize method to make a web service and show the data in JSON format.I used the .json extension at the end of the URL to show this data.Now i want to show this data in table.Output image is attached.Is this possible then how i can do it?
Thanks
The format is a bit odd. I would prefer something like: "task_list": [ .... ]; iterating over objects is always a bit tedious.
Here is the jQuery code:
var data = ...;
var items = data["task_list"];
var table = $('<table/>');
table.appendTo($('body'));
$.each(items, function(id, value) {
var tr = $('<tr/>');
table.append(tr);
$('<td/>').text(id).appendTo(tr);
$('<td/>').text(value).appendTo(tr);
});

Flash: Parsing a JSON file that has a node called Class

Essentially I'm trying to parse JSON and assigning the results to variables, one of which is "var = JSON.class;" with class being what's returned in the JSON. However, flash won't let me parse it because it's called class which it uses to create new classes. Is there any workaround or will I not be able to grab this node?
I'm not sure if this is what you're asking, but if you have a property in your JSON object named "class", you should be able to parse it like as shown below.
// assuming JSON object looks like this, and is stored in a var named 'jsonData'
var jsonData:Object = { "id": 0, "class": "MyClassName", "values": [1,2,3] }
// trying to parse like this won't work w/keywords like 'class':
var parsedValue : String = jsonData.class;
// parse it with this way, using the square brackets:
var parsedValue : String = jsonData["class"];

How to get the name of the token

I need to parse json files which look as follows:
{"20120101":{"Jeff":{"Status":"Sleepy", "Weight":212}, "Cathy":{"Status":"Angry", "Weight":172}}
{"20120102":{"Jeff":{"Status":"Alert", "Weight":207}, "Cathy":{"Status":"Sick", "Weight":168}}
I cannot figure out a way to extract the dates (20120101 and 20120102) and names (Jeff and Cathy) from my json. My attempts look as follows:
private void LoadFile(string fileName)
{
var json = File.ReadAllText(fileName);
JObject days = JObject.Parse(json);
foreach (var dayAsObject in days)
{
var day = (JToken) dayAsObject;
var a = day.Root.ToString();
var t = day.ToString();
var z = day.First;
Console.WriteLine(day+t+z+a);
}
Better formulated json would look like
{"20120101":{ "name":"Jeff", "Status":"Sleepy", "Weight":212}, { "name":"Cathy", "Status":"Angry", "Weight":172}}
Then it is very easy to get day["name"]. I would suggest modifying your json. If you absolutely cant, I think the property you're looking for is PropertyName.
I tend to use the built in System.Web.Script.Serialization JSON libary. If you don't need to do fancy stuff it works great with the dynamic object type.