Home > Net >  How to convert string to json and vice versa in flutter?
How to convert string to json and vice versa in flutter?

Time:07-11

Hi below is the json that I need to extract data from.

[
  "http://activepeersai.computing.dcu.ie/feedback_participant/114",
  "http://activepeersai.computing.dcu.ie/peerLearningPrompter/4",
  "{\"0\":{\"feedback_id\":114,\"timer_used\":1,\"timer\":10.0,\"question1\":\"How to break ice with strangers (in social gathering & in formal events) ?\",\"question2\":\"How to build networking (with those from different age, culture, education background, language\\u2026)(in university & in workplace) ?\",\"question3\":\"\",\"question4\":\"\",\"question5\":\"\",\"question6\":\"\",\"question7\":\"\",\"question8\":\"\",\"question9\":\"\",\"question10\":\"\"},\"1\":{\"feedback_id\":115,\"timer_used\":1,\"timer\":10.0,\"question1\":\"How to deal with difficult teammates (dominating \\/ debater character \\/ negative \\/ lack of confidence \\/ free rider\\u2026) ?\",\"question2\":\"How to build mutual trust with teammates ?\",\"question3\":\"\",\"question4\":\"\",\"question5\":\"\",\"question6\":\"\",\"question7\":\"\",\"question8\":\"\",\"question9\":\"\",\"question10\":\"\"}}",
  "{\"id\":{\"0\":1,\"1\":2,\"2\":3,\"3\":4,\"4\":5,\"5\":6,\"6\":7,\"7\":8,\"8\":9,\"9\":10,\"10\":11,\"11\":12,\"12\":13,\"13\":14,\"14\":15,\"15\":16,\"16\":17,\"17\":18,\"18\":19,\"19\":20,\"20\":21,\"21\":22,\"22\":23,\"23\":24,\"24\":25,\"25\":26,\"26\":27,\"27\":28,\"28\":29,\"29\":30,\"30\":31,\"31\":32,\"32\":33,\"33\":34,\"34\":35,\"35\":40,\"36\":41,\"37\":42,\"38\":43,\"39\":44,\"40\":45,\"41\":46,\"42\":47,\"43\":48,\"44\":49,\"45\":50,\"46\":51,\"47\":52,\"48\":53,\"49\":54,\"50\":55,\"51\":56,\"52\":57,\"53\":58,\"54\":59,\"55\":60,\"56\":61,\"57\":62,\"58\":63,\"59\":36,\"60\":37,\"61\":38,\"62\":39},\"name\":{\"0\":\"Why did the sharer choose this skill as the most confident skill and why did the learner wants to improve this skill?\",\"1\":\"Can you share another situation whereby using this skill made a very big difference to the outcome? What did you do? What was the result? What might have happened if this skill wasn't brought to the situation?\",\"2\":\"What advice do you have to give to others about becoming better at using this skill in their work and in their lives generally?\",\"3\":\"What can hold people back from being better at this skill? How can you encourage them to overcome these challenges?\",\"4\":\"What would be the outcome if this session\\/series was a huge success? How would we know if this happened?\",\"5\":\"What is working for you now? What is stopping you from moving forward?\",\"6\":\"What do your role models do that you would love to learn\\/incorporate as habits\\/adopt as a mindset?\",\"7\":\"\\\"What got you here won't get you there.\\\" What is your reaction to that statement? What actions have got you to this point that may not serve you if you move forward? What new behaviours do you need to adopt?\",\"8\":\"What would be the outcome if this session\\/series was a huge success? How would we know if this happened?\",\"9\":\"If you had all the time, people, money, resources that you might possibly need, what would you do differently? Does that energise you, frighten you or a bit of both?\",\"10\":\"What do you think you need to do get a better result (or closer to your goal)?\",\"11\":\"What is the worst that could happen and how could you handle it? What is the best that could happen and how could you handle it?\",\"12\":\"What have been the most impactful decisions that you've made in your career? What led to making those decisions? Were you aware of the impact those decisions would have? What would you say to others in a similar position?\",\"13\":\"What habits have stood you in good stead? Are there things that you do regularly, daily or very often that have made a big different over time?\",\"14\":\"Can you share an experience that was transformational for you, taught you a long-lasting lesson or was particularly memorable for its benefits and challenges?\",\"15\":\"How do you think experience affects somebody's perspective, way of making decisions or their feelings about taking risks?\",\"16\":\"Can you describe your target market for your business or who might your ideal employer be?\",\"17\":\"Can you share details of a project that you worked on recently? Why were you or your business chosen to work with this client\\/employer? What were the success and challenges along the way?\",\"18\":\"What makes you\\/your product\\/your service different to others? Why would a client or employer choose what you do over others?\",\"19\":\"What might I be able to do to help you?\",\"20\":\"What is the key problem that you are facing currently?\",\"21\":\"If that problem was solved, what impact would it have?\",\"22\":\"In order to get to that outcome, what do we need to do, what resources need to be invested and any other changes to be made?\",\"23\":\"Is there a willingness to take that action to get towards that outcome?\",\"24\":\"What assumptions are being made? How likely are each one to happen? What would be the impact of those assumptions happening or not happening?\",\"25\":\"What application of the SCAMPER technique could be useful to help get another perspective?\\n*SCAMPER is an acronym formed from the abbreviation of: Substitute, Combine, Adapt, Modify (Also magnify and minify), Put to another use, Eliminate and Reverse.\",\"26\":\"If we had double the budget, half the time and were living in another country trying to make this happen, how might we think differently?\",\"27\":\"If any idea generated already in this discussion was to become a reality, what would be the impact on e year later in terms of benefits, ongoing coasts, sustained behavioural change and a launchpad for further growth?\",\"28\":\"Leadership\",\"29\":\"Communication\",\"30\":\"Adaptability\",\"31\":\"Team Work\",\"32\":\"Problem Solving\",\"33\":\"Conflict Management\",\"34\":\"Productivity\",\"35\":\"How would you describe good\\/effective communication? Please share an example of a time that you've seen it in action and an example of when you saw that good communication skills were clearly lacking.\",\"36\":\"How have you handled working under someone you felt was not good at communicating?\",\"37\":\"If you're trying to get your point across or convince somebody that your idea is the right one, what do you do?\",\"38\":\"Who do you think is a good communicator and why? What can we learn from them?\",\"39\":\"Talk about a time that you needed to adapt to a new situation. What did you find difficult and how did you work through that? How can somebody prepare to be more adaptable in future situations?\",\"40\":\"When you're in a situation where it feels like you have no control over it (i.e. a new manager, starting in a new job, government-led changes etc), what do you do to focus on what you can do?\",\"41\":\"How do you handle having multiple priorities at the same time?\",\"42\":\"How do you adjust to different work settings? For example, working with different teams, switching between logic and creativity, learning new processes, tools or technologies?\",\"43\":\"How do you feel about working in a team? What do you think are the key things that need to happen to make good teamwork?\",\"44\":\"What has been your experience of working in teams where there were problems? Did these arise due to strong personalities, somebody not sharing the workload, miscommunication, the wrong support or technology systems etc?\",\"45\":\"How do you keep a team motivated? Share your story about a rewarding team experience.\",\"46\":\"When you're in a team situation, what role do you usually play?\",\"47\":\"Describe a situation where you had to solve a problem. What did you do? what was the result? What might you have done differently?\",\"48\":\"What steps do you take before making a decision on how to solve a problem, and why?\",\"49\":\"Give an example of a situation in which you saw an opportunity in a potential problem. What did you do? What was the outcome?\",\"50\":\"Can you tell me about a situation where you overcame a problem using a creative solution?\",\"51\":\"Have you ever had a team member who kept raising objections on projects? How did you (or would you) manage them?\",\"52\":\"You have noticed that a team member is aggressive or arrogant toward the rest of the team. How would you approach this person?\",\"53\":\"What would you do if your manager gave you negative feedback on the way you approached a problem? How do give negative\\/constructive feedback to others?\",\"54\":\"How could you use a situation with conflict to have a better relationship with all involved?\",\"55\":\"How would you describe a typical working day in your current role? How you manage importance versus urgency? How do you maintain a work-life balance also?\",\"56\":\"If you have your day planned out to achieve a goal, how do you manage distractions or other things that can happen along the way?\",\"57\":\"What do you think very productive people do differently than others?\",\"58\":\"What holds people back from being more productive? How can people turn those around so that by doing the opposite, they can become more productive?\",\"59\":\"Have you ever had a team member who kept raising objections on projects? How did you (or would you) manage them?\",\"60\":\"How do you describe your leadership style?\",\"61\":\"What was a difficult decision you had to make as a leader, and how did you come to that decision?\",\"62\":\"What are the most important attributes of successful leaders today? Who do you think is a good leader and why?\"},\"session_type_id\":{\"0\":1.0,\"1\":1.0,\"2\":1.0,\"3\":1.0,\"4\":2.0,\"5\":2.0,\"6\":2.0,\"7\":2.0,\"8\":3.0,\"9\":3.0,\"10\":3.0,\"11\":3.0,\"12\":4.0,\"13\":4.0,\"14\":4.0,\"15\":4.0,\"16\":5.0,\"17\":5.0,\"18\":5.0,\"19\":5.0,\"20\":6.0,\"21\":6.0,\"22\":6.0,\"23\":6.0,\"24\":7.0,\"25\":7.0,\"26\":7.0,\"27\":7.0,\"28\":8.0,\"29\":8.0,\"30\":8.0,\"31\":8.0,\"32\":8.0,\"33\":8.0,\"34\":8.0,\"35\":null,\"36\":null,\"37\":null,\"38\":null,\"39\":null,\"40\":null,\"41\":null,\"42\":null,\"43\":null,\"44\":null,\"45\":null,\"46\":null,\"47\":null,\"48\":null,\"49\":null,\"50\":null,\"51\":null,\"52\":null,\"53\":null,\"54\":null,\"55\":null,\"56\":null,\"57\":null,\"58\":null,\"59\":null,\"60\":null,\"61\":null,\"62\":null},\"session_type_name\":{\"0\":\"Learning and Development\",\"1\":\"Learning and Development\",\"2\":\"Learning and Development\",\"3\":\"Learning and Development\",\"4\":\"Mentoring\",\"5\":\"Mentoring\",\"6\":\"Mentoring\",\"7\":\"Mentoring\",\"8\":\"Coaching\",\"9\":\"Coaching\",\"10\":\"Coaching\",\"11\":\"Coaching\",\"12\":\"Experience Sharing\",\"13\":\"Experience Sharing\",\"14\":\"Experience Sharing\",\"15\":\"Experience Sharing\",\"16\":\"Networking\",\"17\":\"Networking\",\"18\":\"Networking\",\"19\":\"Networking\",\"20\":\"Establishing Buy-In\",\"21\":\"Establishing Buy-In\",\"22\":\"Establishing Buy-In\",\"23\":\"Establishing Buy-In\",\"24\":\"Idea Exploration\",\"25\":\"Idea Exploration\",\"26\":\"Idea Exploration\",\"27\":\"Idea Exploration\",\"28\":\"Seven Soft Skills\",\"29\":\"Seven Soft Skills\",\"30\":\"Seven Soft Skills\",\"31\":\"Seven Soft Skills\",\"32\":\"Seven Soft Skills\",\"33\":\"Seven Soft Skills\",\"34\":\"Seven Soft Skills\",\"35\":null,\"36\":null,\"37\":null,\"38\":null,\"39\":null,\"40\":null,\"41\":null,\"42\":null,\"43\":null,\"44\":null,\"45\":null,\"46\":null,\"47\":null,\"48\":null,\"49\":null,\"50\":null,\"51\":null,\"52\":null,\"53\":null,\"54\":null,\"55\":null,\"56\":null,\"57\":null,\"58\":null,\"59\":null,\"60\":null,\"61\":null,\"62\":null},\"parent_id\":{\"0\":null,\"1\":null,\"2\":null,\"3\":null,\"4\":null,\"5\":null,\"6\":null,\"7\":null,\"8\":null,\"9\":null,\"10\":null,\"11\":null,\"12\":null,\"13\":null,\"14\":null,\"15\":null,\"16\":null,\"17\":null,\"18\":null,\"19\":null,\"20\":null,\"21\":null,\"22\":null,\"23\":null,\"24\":null,\"25\":null,\"26\":null,\"27\":null,\"28\":null,\"29\":null,\"30\":null,\"31\":null,\"32\":null,\"33\":null,\"34\":null,\"35\":30.0,\"36\":30.0,\"37\":30.0,\"38\":30.0,\"39\":31.0,\"40\":31.0,\"41\":31.0,\"42\":31.0,\"43\":32.0,\"44\":32.0,\"45\":32.0,\"46\":32.0,\"47\":33.0,\"48\":33.0,\"49\":33.0,\"50\":33.0,\"51\":34.0,\"52\":34.0,\"53\":34.0,\"54\":34.0,\"55\":35.0,\"56\":35.0,\"57\":35.0,\"58\":35.0,\"59\":29.0,\"60\":29.0,\"61\":29.0,\"62\":29.0}}"
]

I need to extract data from the 3rd line. The below code tries to extract the first set of questions from the 3rd line.

import 'dart:core';
import 'dart:convert';
import 'package:http/http.dart' as http;
class Question {
  late final String question;
  Question({ required this.question});
}

void getData() async{
  List<Question> sample_data = [];
  String link = 'https://activepeersai.computing.dcu.ie/API/631';
  var url = Uri.parse(link);
  var result = await http
      .get(url,headers: {"Accept": "application/json","Access-Control_Allow_Origin": "*"});
  if(result.statusCode==200){
    var data = jsonDecode(result.body);
    var res = jsonDecode(data[2]);
    String round1 = json.encode(res);
    var data1 = jsonDecode(round1);
    var questions = jsonDecode(data1[0]);
    for(var k in questions.keys){
      if(((k=='question1')|| (k=='question2') || (k=='question3') || (k=='question4') || (k=='question5') || (k=='question6') || (k=='question7') || (k=='question8') || (k=='question9') || (k=='question10'))){
        Question question = Question(question: res[k]);
        sample_data.add(question);
      }
    }
    print('$sample_data');
  }
}
void main() {
  getData();
}

Now when I print round1 I get the below output.

{"0":{"feedback_id":114,"timer_used":1,"timer":10,"question1":"How to break ice with strangers (in social gathering & in formal events) ?","question2":"How to build networking (with those from different age, culture, education background, language…)(in university & in workplace) ?","question3":"","question4":"","question5":"","question6":"","question7":"","question8":"","question9":"","question10":""},"1":{"feedback_id":115,"timer_used":1,"timer":10,"question1":"How to deal with difficult teammates (dominating / debater character / negative / lack of confidence / free rider…) ?","question2":"How to build mutual trust with teammates ?","question3":"","question4":"","question5":"","question6":"","question7":"","question8":"","question9":"","question10":""}}

Which when I checked on Json Checker came to be valid. But after this I get an error.

Error: Expected a value of type 'String', but got one of type 'Null'
    at Object.throw_ [as throw] (http://localhost:56003/dart_sdk.js:5080:11)
    at Object.castError (http://localhost:56003/dart_sdk.js:5039:15)
    at Object.cast [as as] (http://localhost:56003/dart_sdk.js:5356:17)
    at String.as (http://localhost:56003/dart_sdk.js:46240:19)
    at getData (http://localhost:56003/packages/testing_for_data/main.dart.lib.js:74:56)
    at getData.next (<anonymous>)
    at http://localhost:56003/dart_sdk.js:40641:33
    at _RootZone.runUnary (http://localhost:56003/dart_sdk.js:40511:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:56003/dart_sdk.js:35438:29)
    at handleValueCallback (http://localhost:56003/dart_sdk.js:35999:49)
    at _Future._propagateToListeners (http://localhost:56003/dart_sdk.js:36037:17)
    at [_completeWithValue] (http://localhost:56003/dart_sdk.js:35872:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:56003/dart_sdk.js:35906:35)
    at Object._microtaskLoop (http://localhost:56003/dart_sdk.js:40778:13)
    at _startMicrotaskLoop (http://localhost:56003/dart_sdk.js:40784:13)
    at http://localhost:56003/dart_sdk.js:36261:9

Can someone tell me what I am doing wrong? Its coming due to the line

var data1 = jsonDecode(round1);

CodePudding user response:

I believe this line is wrong

var questions = jsonDecode(data1[0]);

Firstly, because you probably need data1['0'] instead of data1[0]. They key is a string, not a number.

Secondly. You don't need to decode it again. It's already decoded. So try

var questions = data1['0'];

The next thing is that

Question question = Question(question: res[k]);

needs to be changed to

Question question = Question(question: questions[k]);

Because you actually want it from the questions.

If you want it to print the question instead of Instance of 'Question' you can add a toString method to it, for example:

class Question {
  late final String question;
  Question({ required this.question});

  @override
  String toString() {
    return question;
  }
}
  • Related