Page 1 of 1

Programm issue

Posted: Tue Oct 25, 2011 9:18 pm
by phoenixb
Hi,

Question, i'm trying for to made a script working for the Arduino but i can not find the solution so can some one give me the trick.

I have the next part of the script to made a JSON string:

Code: Select all

    str.print("{");                                   
    for(int i=0;i<16; i++)
    {
      if (wiretx.address[i][0] || wiretx.address[i][1])
      { 
        str.print("%22temp_");
        for (uint8_t p = 0; p < 2; p++)
        {
          str.print(wiretx.address[i][p],HEX);
        } 
        str.print("%22");
        str.print(':');
        str.print("%22");
        str.print(wiretx.value[i]/100.0);
        str.print("%22");
        str.print(",");
        str.print("}");
      }
    }
Ok, it is not the prettiest solution but it works almost.

The only thing i have is that on the and of the script where i can not anymore fill the string i want that he is not closing the string with a , but with the last value.
How can i do that?
I like to hear it.
Regards,

Re: Programm issue

Posted: Tue Oct 25, 2011 9:50 pm
by Digit
1.
Shouldn't the str.print("}"); be outside of the i loop?
Cause it looks like you get 1 * { and lots of }'s, right?

2.
After the i loop, if str ends with a comma, delete it.

3.
After that, add the } you removed at (1).

4,
1 other thing: the result can be just a single {. Not good either I guess

Re: Programm issue

Posted: Wed Oct 26, 2011 12:05 pm
by phoenixb
Hi,

1) I see that i have paste the str.print("}"); on the wrong place here in the forum, in my script it's outside the loop.
2) Ok, but how can i made it that the , that i need between the values because the string is build like -> {"key1":"value","key2":"value","key3":"value"}
3) see 1, but i will try it tonight to made this a little bit other
4) What do you mean by that?

I like to hear it.
Regards,

Re: Programm issue

Posted: Wed Oct 26, 2011 12:29 pm
by Digit
2) leave the addition of the comma where it is and strip the last one, if necessary.
4) look at your own code as you posted it here. If all if's result in false, str will contain "{". I would think "" or "{}" would be better.

Re: Programm issue

Posted: Thu Oct 27, 2011 8:36 am
by phoenixb
Hi,

Thanks for your info, ik have solved the most of the problems.
And I must say I think I do understand the logic a little bit.

Only 1 part i i can not solve and that is the , part.

When i instert the , i get this string -> {"key1":"value","key2":"value","key3":"value",}
And i must have this -> {"key1":"value","key2":"value","key3":"value"}

But when i delete the , on the end i get this -> {"key1":"value""key2":"value""key3":"value"}
So i must have a way to delete the , on the last value.

Is this possible?
I like to hear it.
Regards,

Re: Programm issue

Posted: Thu Oct 27, 2011 9:19 am
by Digit
Adding a comma is only needed when you add a 2nd, 3rd,... key/value pair.
Just think about how you decide whether you need to add a comma or not.
Q:When do you have to add a comma?
A: when you're going to add a next key/value pair.
Pseudo code:

Code: Select all

str=""
nex=false
for i = 1 to 10
{
  str=str+iif(nex,",","")+string(i)
  nex=true
}
This will give you a string "1,2,3,4,5,6,7,8,9,10"
(but it doesn't compile, that's for you to do - I didn't want to take away the fun part :wink: )

Re: Programm issue

Posted: Thu Oct 27, 2011 10:28 am
by phoenixb
Hi,

I think i can something with this, i will try tonight to change some rules.
I will let it know how i have made the solution.

Grtz,