Convert json into Map in Dart and Futter [duplicate] - json

This question already has answers here:
How to decode JSON in Flutter?
(6 answers)
Closed 4 months ago.
I am getting a list of this type of json from a API. but I want to convert into a map with only its name, quoteCount, and slug.
{
"_id": "_gz-cfmqA1mr",
"name": "Alexander Pope",
"link": "https://en.wikipedia.org/wiki/Alexander_Pope",
"bio": "Alexander Pope (21 May 1688 – 30 May 1744) is regarded as one of the greatest English poets, and the foremost poet of the early eighteenth century. He is best known for his satirical and discursive poetry, including The Rape of the Lock, The Dunciad, and An Essay on Criticism, as well as for his translation of Homer.",
"description": "English poet",
"quoteCount": 3,
"slug": "alexander-pope",
"dateAdded": "2020-12-19",
"dateModified": "2020-12-19"
}
How to convert this json into Map?

try this
import 'dart:convert'
Map<String,dynamic> map = jsonDecode(responseJson);

Try this
final response = await http.get(your url);
final Map<String, dynamic> data = json.decode(response.body);

Related

Jsp use JSON Object with JSTL [duplicate]

This question already has answers here:
How do I use JSTL to output a value from a JSON string?
(1 answer)
Display JSON object using JSP/ JSTL tags in UI? [duplicate]
(1 answer)
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
I know this topic was discussed pretty often and I guess it is not a diffucult thing.
I want to use a JSON Object from my session in a JSP. The Object has the following structur:
{
"addedUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
}
],
"allUsers":[
{
"city":"Los Angeles",
"name":"Doe",
"forname":"John",
"title":"Dr.",
"userId":2
},
{
"city":"New York",
"name":"Peter",
"forname":"Parker",
"title":"Dr.",
"userId":3
}
]
}
Now I want to grab the Objects by name for example doing a for each on the "addedUsers" Object and grab the properties. It is important not just to iterate over the whole Object. I have to call them by name.

How to look up values in a large JSON file in Dart

I have a large JSON file with many objects with many properties. Simplified structure looks like this:
"allGadgets": [
{
"Model Code": "nokia1",
"Top Category": "Mobile Phones",
"Category": "non-iPhone",
"Brand": "Nokia",
"Device": "1",
"Price": "£ 11"
},
{
"Model Code": "nokia2",
"Top Category": "Mobile Phones",
"Category": "non-iPhone",
"Brand": "Nokia",
"Device": "2",
"Price": "£ 17",
},
{
"Model Code": "nokia3",
"Top Category": "Mobile Phones",
"Category": "non-iPhone",
"Brand": "Nokia",
"Device": "3",
"Price": "£ 10",
}] ... plus a few hundreds more of different brands and models
I'm extracting from this json list of maps a list of Strings for a search panel for the user to look up their device. The Strings are made of two of the values from the json, i.e.: "${item['Brand']} - ${item['Device']}"
Once the user has selected the relevant model from the dropdown search panel, I need to use this string value to give them the price from the json file. The question is how do I achieve that in dart/flutter? If it was html/css, I would have added an extra hidden field of model code and/or price itself and then just made it visible.
In flutter/dart, however, a search panel plugin I found only accepts Strings, which the user selects and which then have to be used to look up the the corresponding price value in the json file.
Complicating the lookup is the fact that my Strings are now composed of two field values with spaces and a hyphen in between so I probably need to convert them back into how they had been prior to the string conversion and then use both for the lookup... which sounds quite convoluted...
Any thoughts on how to solve the above task would be welcome!
What I guess would help a lot is an example - looking up an object using a String (formed from two values from the objects) within a json with many objects. User is presented with a subset of those objects, but just sees a couple of fields from them. Then the user effectively selects a query using the String shown to him based off the two fields. That String then allows to look up the object and find another value (price) in that corresponding object...
Having decoded your json, you have a List of Maps. Make a new data structure which is a Map of Maps (i.e. Map<String, Map<String, dynamic>>). Populate the new Map by adding each member of the List, keyed by the brand/device name. Now you can directly look up the device details by that composite name.
List<Map<String, dynamic>> original;
Map<String, Map<String, dynamic>> data = {};
original.forEach((item) {
String brandDeviceName = '${item['Brand']} - ${item['Device']}';
data[brandDeviceName] = item;
});

Multiline string as a value in Json [duplicate]

This question already has answers here:
Are multi-line strings allowed in JSON?
(15 answers)
Closed 4 years ago.
Given this Json
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\n
he is taking maths\ n
biology this semester "
}
}
I get a Json parser exception for 'description' in jsonlint. Changing the description to accept an array is not an option for me.
Is there any way to define multiline in Json?
\n is the only way. JSON isn't programming language and you can't tell it to concatenate string. It depends on context too.
This will work
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\nhe is taking maths\nbiology this semester"
}
}
This will not work
{
"myclass": {
"studentname": "myname",
"description": "Student has three more credits to complete\n
he is taking maths\n
biology this semester"
}
}

How to get value from specific key in NodeJS JSON [duplicate]

This question already has an answer here:
Getting value from object in javascript
(1 answer)
Closed 6 years ago.
{
"id": 83,
"key": "hello",
"en": "Hello",
"mm": "This is greeting",
"created_at": "2016-12-05T10:14:02.928Z",
"updated_at": "2017-01-31T02:57:11.181Z"
}
I'm trying to get value from mm key in NodeJS. I've tried to get translation["mm"] but it does not work at all. Please help me how to get mm from above data.
My current node version is 5.11
Edited The Answer. Now Its working for above object in question
You can use following function to access the keys of JSON. I have returned 'mm' key specifically.
function jsonParser(stringValue) {
var string = JSON.stringify(stringValue);
var objectValue = JSON.parse(string);
return objectValue['mm'];
}
JSON is an interchange format, meaning it's only a text representing data in a format that many applications can read and translate into it's own language.
Therefore, using node.js you should parse it first (translate it to a javascript object) and store the result in a variable:
var obj = JSON.parse(yourJSONString);
Then, you can ask for any of it properties:
var mm = obj.mm;

Deserialize nested json in VB.NET [duplicate]

This question already has answers here:
VB.net JSON Deserialize
(3 answers)
Closed 6 years ago.
I am using the json.net library and I have this json file I want to deserialize:
{
"location":"/UndefinedTag/UndefinedPos/1480679543072",
"parameters":{
"SwitchPoint1":{
"SamplingRate":null,
"BitLength":16,
"BitOffset":0,
"DataType":"BooleanT"
},
"SwitchPoint2":{
"SamplingRate":null,
"BitLength":16,
"BitOffset":1,
"DataType":"BooleanT"
},
"SensorValue":{
"SamplingRate":null,
"BitLength":14,
"BitOffset":2,
"DataType":"IntegerT"
}
}
}
can anybody help ?
Thank you
Please read Deserializing complex object using Json.NET first (altough it's C#-related). In your case you need several .NET object types defined beforehand:
"parent" one holding location and parameters properties (where parameters represents collection of "named sensors")
"named sensor" representing each specialized sensor type, such as SwitchPoint1, SwitchPoint2 or SensorValue (where each "named sensor" type contains related "sensor value" property)
"sensor value" holding single sensor properties such as SamplingRate, BitLength, etc.