Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Unanswered

issue with iterate json array with RetailCommonWebAPI

(0) ShareShare
ReportReport
Posted on by 1,871

Hello,

I have two json arrays in below code. The first json array I called _jsonStrone and I don't have any issue iterating the values. But in second _jsonStrtwo array I have issue  to iterate the values of array and my real api doesn't contain any main key value to use in switch function to create a case. So I need iterate any element of array _jsonStrtwo and print the results even array doesn't contain key value like _jsonStrone as "branchList". Please advise.

static void readJSON(Args _args)
{
mapEnumerator mapEnumerator;
Map data;
List testList;
str _jsonStrone, _jsonStrtwo;
container test;
ListEnumerator listEnumerator;
int containerList;
Map map1;
int lengthIterate;
str _conpkrsult;

;
 _jsonStrone = '{"branchList":["11102206","12104348","603","604","605","606","607"]}';
 _jsonStrtwo = '[{"name":"Harry Potter","city":"London"},{"name":"Don Quixote","city":"Madrid"},{"name":"Joan of Arc","city":"Paris"},{"name":"Rosa Park","city":"Alabama"}]';
 
  
data = RetailCommonWebAPI::getMapFromJsonString(_jsonStrone); //_jsoStrtwo. 
mapEnumerator = data.getEnumerator();
while (mapEnumerator.moveNext())
{
switch (mapEnumerator.currentKey())
{
case "branchList" :
test = mapEnumerator.currentValue();
testList =List::create(test);
listEnumerator = testList.getEnumerator();
while (listEnumerator.moveNext())
{
test = listEnumerator.current();

 _conpkrsult = conpeek(test, 1);

  print _conpkrsult;
    pause;
}
break;
}
}
   
}

  • Martin Dráb Profile Picture
    Martin Dráb 230,433 Most Valuable Professional on at
    RE: issue with iterate json array with RetailCommonWebAPI

    And I already mentioned quite different way how to use the library - one is generating classes for your JSON and deserializing JSON to those classes, and one deserializing to JObject. You can also use the library either from X++ through .NET Interop, or creating a C# class library and doing the work there, which especially useful when you want to use "Generate classes from JSON" and utilize generic types and methods.

    The blog post shows deserialization to JObject through .NET Interop, but it's impossible to say whether it's what you need unless you specify your requirements. It would be a mistake to think that this is the only option.

  • Faqruddin Profile Picture
    Faqruddin 1,871 on at
    RE: issue with iterate json array with RetailCommonWebAPI

    Please read my comment above. You advise the most common used library to deserialize Newtonsoft.Json. I request in my last comment if you know the approach please advise with an example . I found an example below is a link and I hope it help someone. So no need more explanations. Appreciated your time. Thank you.

    axgrind.azurewebsites.net/.../

  • Martin Dráb Profile Picture
    Martin Dráb 230,433 Most Valuable Professional on at
    RE: issue with iterate json array with RetailCommonWebAPI

    An example of what exactly? I don't to waste with explaining a scenario that you aren't actually interested in.

  • Faqruddin Profile Picture
    Faqruddin 1,871 on at
    RE: issue with iterate json array with RetailCommonWebAPI

    Let me see System.Web.Script.Serialization.JavaScriptSerializer. As I see the method it supporting array of objects. But even I don't know how its deserialize the JSON array elements with RetailCommonWebAPI complex json array structures. I hope some one know this and comment it. Regarding Newtonsoft.Json library please if you know how to attach to ax 2012 r3. Please guide with an example.

    Thank you

  • Martin Dráb Profile Picture
    Martin Dráb 230,433 Most Valuable Professional on at
    RE: issue with iterate json array with RetailCommonWebAPI

    If you open RetailCommonWebAPI, you'll see that it uses System.Web.Script.Serialization.JavaScriptSerializer. You could use that directly. I guess that it can deserialize arrays (but I don't use it, therefore I don't know).

    The most common library for this purpose is Newtonsoft.Json. If you know the structure up front, you can easily generate classes to deserialize the JSON to and then work with the data in strongly-typed manner. If you don't, you can deserialize JSON to a JObject and use its methods to manipulate the data.

    As always, there is no single approach that is the best in all cases. The choice depends on your particular situation.

  • Faqruddin Profile Picture
    Faqruddin 1,871 on at
    RE: issue with iterate json array with RetailCommonWebAPI

    Thanks martin. Please if you know any alternative way without using RetailCommonWebAPI methods, Please suggest. I need to read any json format regardless of structure.

  • Martin Dráb Profile Picture
    Martin Dráb 230,433 Most Valuable Professional on at
    RE: issue with iterate json array with RetailCommonWebAPI

    Please add line indentation to your code. It's very important for readability, and you decrease your chances for getting an answer if you make it complicated for us.

    Anyway, using getMapFromJsonString() looks wrong if the JSON don't contain a map. It represents an array of objects.

    I don't see any suitable method in RetailCommonWebAPI, therefore either don't use it, add such a method or modify your JSON string to give it the structure expected by getMapFromJsonString().

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,703 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans