0

Im trying to convert a string into list of object in dart. The string look like this:

What it looks like in my app - image

The JSON return - pastebin

My class model:

class Post {
  GetDataResult getDataResult;

  Post({this.getDataResult});

  Post.fromJson(Map<String, dynamic> json) {
    getDataResult = json['GetDataResult'] != null
        ? new GetDataResult.fromJson(json['GetDataResult'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.getDataResult != null) {
      data['GetDataResult'] = this.getDataResult.toJson();
    }
    return data;
  }
}

class GetDataResult {
  String retVal;

  GetDataResult({this.retVal});

  GetDataResult.fromJson(Map<String, dynamic> json) {
    retVal = json['RetVal'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['RetVal'] = this.retVal;
    return data;
  }
}

Can anyone help me?

1
  • You can convert your json array into list of object such as click here Commented Jan 31, 2020 at 5:51

4 Answers 4

0

You need to use gson, convert your string into list of object, gson.fromJson() method return what you want json format :

SomeObject[] yourJson = gson.fromJson(jsonObject.toString(), SomeObject[].class);

Please try above can, if you still facing issues share your error.

Sign up to request clarification or add additional context in comments.

Comments

0

To convert the Json To String. For Example,

class sample {String name} print(sample.name.toString());

Comments

0

I solved it. First I create a new class model:

class Profesi {
  String id;
  String description;
  String idPekerjaan;

  Profesi({this.id, this.description, this.idPekerjaan});

  Profesi from ({object: Map}) {
    var map = object;
    return Profesi(
      id: map['ID'] as String,
      description: map['Description'] as String,
      idPekerjaan: map['IDPekerjaan'] as String,
    );
  }
}

in my postRequest method I prepare a variable to contain decoded json from my retVal:

List listJsonContent;
listJsonContent = json.decode(Post.fromJson(json.decode(response.body)).getDataResult.retVal);

Prepare a list of my new Profesi class and insert the object using add() method (here I insert just one: listJsonContent[0], you can insert all by iterate the list):

List<Profesi> profesi = List<Profesi>();
profesi.add(Profesi().from(object: listJsonContent[0]));

Try to print it to check:

print('${profesi[0].id}, ${profesi[0].idPekerjaan}, ${profesi[0].description}');

Comments

-1

Use this link to create the Dart Object from Json.

Visit https://javiercbk.github.io/json_to_dart/ this!

1 Comment

No. I need to convert the string value from JSON object. I already made the class model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.