Working with JSON in dart
You,•dart + flutter
TLDR;
- objects in Dart are strongly typed
- the library you need is: import 'dart:convert';
- the function you need is: json.decode('json string goes here');
- when you parse objects, you get a standard JSArray
object - you need a couple more steps to get this into a strongly typed object
code that works
import 'dart:convert';
void main()
{
List oo = json.decode('[{"i":737,"n":1},{"i":222,"n":111}]');
print(oo);
print(oo.runtimeType);
List uoo = json.decode('[1,4,5,10,11]');
print(uoo);
print(uoo.runtimeType);
print(uoo.length);
for(int i =0; i< uoo.length;i++)
{
print("value of uoo[$i] = ${uoo[i]}");
}
var arreyBhai =[11,22,44,67,33];
print(arreyBhai.length);
for(int i =0; i< arreyBhai.length;i++)
{
print("value of arreyBhai[$i] = ${arreyBhai[i]}");
}
}
Output:
[{i: 737, n: 1}, {i: 222, n: 111}]
JSArray<dynamic>
[1, 4, 5, 10, 11]
JSArray<dynamic>
5
value of uoo[0] = 1
value of uoo[1] = 4
value of uoo[2] = 5
value of uoo[3] = 10
value of uoo[4] = 11
5
value of arreyBhai[0] = 11
value of arreyBhai[1] = 22
value of arreyBhai[2] = 44
value of arreyBhai[3] = 67
value of arreyBhai[4] = 33