Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7758295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:19:20+00:00 2026-06-01T13:19:20+00:00

I want to be able to parse a string to an object that I

  • 0

I want to be able to parse a string to an object that I can access using the dot notation e.g. myobject.property, instead of the array notation e.g. myobject[‘property’]. The array notation works fine. Here’s what I have so far.

I have some XML:

<level1 name="level1name">
  <level2 type="level2Type">
    <entry>level2entry</entry>
    <entry>level2entry</entry>
  </level2>
</level1>

Which converts to the JSON:

{
  "level1": {
    "name": "level1name",
    "level2": {
      "type": "level2Type",
      "entry": [
        "level2entry",
        "level2entry"
      ]
    }
  }
}

I have the following Dart code:

Object jsonObject = JSON.parse("""{
      "level1": {
        "name": "level1name",
        "level2": {
          "type": "level2Type",
          "entry": [
            "level2entry",
            "level2entry"
          ]
        }
      }
    }
""");

  print("my test 1 == ${jsonObject}");
  print("my test 2 == ${jsonObject['level1']}");
  print("my test 3 == ${jsonObject['level1']['name']}");

which produce the (desired) output:

my test 1 == {level1: {name: level1name, level2: {type: level2Type, entry: [level2entry, level2entry]}}}
my test 2 == {name: level1name, level2: {type: level2Type, entry: [level2entry, level2entry]}}
my test 3 == level1name

But when I try:

print("my test 1 == ${jsonObject.level1}");

I get the following:

Exception: NoSuchMethodException : method not found: 'get:level1'
Receiver: {level1: {name: level1name, level2: {type: level2Type, entry: [level2entry, level2entry]}}}
Arguments: []
Stack Trace:  0. Function: 'Object.noSuchMethod' url: 'bootstrap' line:717 col:3

Ideally, I want an object that I can access using the dot notation and without the compiler giving warning about Object not having property. I tried the following:

class MyJSONObject extends Object{
  Level1 _level1;
  Level1 get level1() => _level1;
  set level1(Level1 s) => _level1 = s; 
}

class Level1 {
  String _name;
  String get name() => _name;
  set name(String s) => _name = s; 
}
...
MyJSONObject jsonObject = JSON.parse("""{
      "level1": {
        "name": "level1name",
        "level2": {
          "type": "level2Type",
          "entry": [
            "level2entry",
            "level2entry"
          ]
        }
      }
    }
""");
...
print("my test 1 == ${jsonObject.level1.name}");

but instead of giving me ‘level1name’ as hoped, I get:

Exception: type 'LinkedHashMapImplementation<String, Dynamic>' is not a subtype of type 'MyJSONObject' of 'jsonObject'.

What am I doing wrong here? Is there any way to do what I’m trying? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:19:21+00:00Added an answer on June 1, 2026 at 1:19 pm

    At the moment, JSON.parse only returns Lists (array), Maps, String, num, bool, and null
    (api ref).

    I suspect that until reflection makes it way into the language, it won’t be able to re-construct objects based upon the keys found in json.

    You could, however, create a constructor in your MyJsonObject which took a string, called JSON.parse internally, and assigned the various values.

    Something like this works in the dart editor:

    #import("dart:json");
    class Level2 {
       var type;
       var entry;
    }
    
    class Level1 {
       var name;
       var level2;        
    }
    
    class MyJSONObject {
      Level1 level1;
    
    
      MyJSONObject(jsonString) {
         Map map = JSON.parse(jsonString);
    
         this.level1 = new Level1();
         level1.name = map['level1']['name'];
         level1.level2 = new Level2();
         level1.level2.type = map['level1']['level2']['type'];
         //etc...
    
      }
    }
    
    main() {
       var obj = new MyJSONObject(json);
       print(obj.level1.level2.type);
    
    }
    

    A non trivial version would needs some loops and possible recursion if you had deeper nested levels.

    Update: I’ve hacked together a non-trivial version (inspired by the post below), it’s up on github (also taking Seth’s comments re the constructor):

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm want to parse a custom string format that is persisting an object graphs
I want to be able to parse the following times using ParseExact() function: 01:02:03
I want to parse the below given data using resultsRequestSOAP object in order to
I'd like to be able to parse a string of JSON representing an object
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
I am able to parse the HTML but I want to extract the warning
I'm using LaTeX and BibTeX for an article, and I want to able to
So I want to be able to parse, and evaluate, dice expressions in C#.
I am trying to parse a json string straight into a managed object. The
I want to be able to parse this date-time format 2011-12-02T16:18:12.479-05:00 into an mm/dd/yy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.