Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,558 members, 7,823,454 topics. Date: Friday, 10 May 2024 at 10:23 AM

Programming Challenge: Convert String to Json with a Loop - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Challenge: Convert String to Json with a Loop (5427 Views)

Professional Html,bootstrap,php,java,json Web Deloper. Pay After Service! / Jamb Past Questions API In JSON / English Dictionary In Xml/json Format (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Re: Programming Challenge: Convert String to Json with a Loop by nnasino(m): 7:48am On Apr 24, 2016
....
Re: Programming Challenge: Convert String to Json with a Loop by nnasino(m): 7:50am On Apr 24, 2016
Febup:
@nnasino

You are the second winner, well done bro.
Thanks boss
dhtml18:

Typo, i meant actually die in real life by trying to complete this code. Me, just looking at it, am sweating already and getting high-blood pressure.
Lawd have mercy, see this nnasino codes as e long like Lagos - Ibadan expressway.

Lol, Oga dhtml18 na 14 lines o Abi na sarcasm?
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 5:05pm On Apr 24, 2016
Hmn, maybe it is 14 lines, but i got scared so the code looked very looooong in my eyes.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 6:01pm On Apr 24, 2016
dhtml18:
Hmn, maybe it is 14 lines, but i got scared so the code looked very looooong in my eyes.

grin grin grin grin grin Don't be scared bro, Naija's got dangerous programmers and you are one of us, you just need to give the challenge a go and you'll be smiling too with an e-champagne grin .
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 7:07pm On Apr 24, 2016
No o, this kind of challenge is bad for my help, you know i now use a walking stick. As much as i will love to try, i am just too e-old for this.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 7:45pm On Apr 24, 2016
dhtml18:
No o, this kind of challenge is bad for my help, you know i now use a walking stick. As much as i will love to try, i am just too e-old for this.

If you have e-faith as small as a kilobyte grin you can say to your walking stick and the e-old age be cast into cyber space, then you can take this e-challenge like an e-lion.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 10:51pm On Apr 24, 2016
Febup:


If you have e-faith as small as a kilobyte grin you can same to your walking stick and the e-old age be cast into cyber space, then you can take this e-challenge like an e-lion.
Hmn, maybe I can ask seun to change my username to daniel!
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 12:40pm On Apr 25, 2016
dhtml18:

Hmn, maybe I can ask seun to change my username to daniel!

Alright bro, waiting for the change of username from the oga of nairaland, then you can take this challenge.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 5:28pm On Apr 25, 2016
cyrielo:

Oh didn't check the json validation thing
**Modified Code
Note: I made some modification to your string format.
Notice the double carriage return to separate keys from actual values and i removed the new line at the of the string it causes a new row.
Febup:

Nice try but you cannot modify the string, the string has to remain the same for this challenge.

Hope you are okay trying this challenge. Just checking on you as I've not heard back from you for a while.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 9:26pm On Apr 25, 2016
I hear that some people actually fainted when they attempted this challenge.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 9:04am On Apr 26, 2016
dhtml18:
I hear that some people actually fainted when they attempted this challenge.

Only dangerous programmers should attempt this challenge, else I'll advice other programmers to wait till they get to that level.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 11:21am On Apr 26, 2016
Febup:

Only dangerous programmers should attempt this challenge, else I'll advice other programmers to wait till they get to that level.
Yeah, because this kind of thing can result in a real-life death.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 12:37pm On Apr 26, 2016
Now I have concern for 'cyrielo' as there has been no sign of him since Apr 23 after I informed him that "the string has to remain the same for this challenge"

Can someone that knows cyrielo please help us check to know if he is ok.
Re: Programming Challenge: Convert String to Json with a Loop by cyrielo(m): 2:42pm On Apr 26, 2016
Febup:

Now I have concern for 'cyrielo' as there has been no sign of him since Apr 23 after I informed him that "the string has to remain the same for this challenge"
Can someone that knows cyrielo please help us check to know if he is ok.
Are you seriously serious LMAO grin grin Been busy man, will drop the other version this evening..
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 3:09pm On Apr 26, 2016
cyrielo:

Are you seriously serious LMAO grin grin Been busy man, will drop the other version this evening..

Nice to hear from you bro.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 7:01pm On Apr 26, 2016

import java.util.*;

public class Main
{

public static void main(String[] args)
{
String str = "Country\tState\tTown\r\nNigeria\tLagos State\tLagos"
+"\r\nUSA\tTexas\tDallas\r\n"+
"Nigeria\tBauchi State\tToro\r\n"+
"Nigeria\tPlateau State\tApata(jos north)\r\n"+
"Biafra\tAnambra(light house)\tMbaukwu\r\n"+
"Nigeria\tKaduna State\tSaminaka\r\n";
StringBuffer buffer = new StringBuffer();


String[] rows_headers = str.split("\r\n"wink;

String[] headers = rows_headers[0].split("\t"wink;

buffer.append("["wink;
for(int i = 1; i < rows_headers.length; i++){
String[] rows = rows_headers[i].split("\t"wink;
buffer.append("{"wink;

for(int j = 0; j < headers.length; j++){
buffer.append("\n\""+ headers[j] +"\":\"" + rows[j]
+ "\"" + (j<headers.length-1?",":""wink);
}
buffer.append("}"wink;


if( i < rows_headers.length-1)
buffer.append(",\n"wink;

//buffer.append("\n"wink;

}

buffer.append("]"wink;

String json = buffer.toString();


System.out.println(json);

}

}


output:
[{
"Country":"Nigeria",
"State":"Lagos State",
"Town":"Lagos"},
{
"Country":"USA",
"State":"Texas",
"Town":"Dallas"},
{
"Country":"Nigeria",
"State":"Bauchi State",
"Town":"Toro"},
{
"Country":"Nigeria",
"State":"Plateau State",
"Town":"Apata(jos north)"},
{
"Country":"Biafra",
"State":"Anambra(light house)",
"Town":"Mbaukwu"},
{
"Country":"Nigeria",
"State":"Kaduna State",
"Town":"Saminaka"}]

:::MODIFIED::::::


validated

Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 7:36pm On Apr 26, 2016
^^^The programmer above me is a desperado, he is so confident that he can code that he is even thumping his chest and trying to prove that he is the only programmer on this board.
@op, please help him to debug his code (and prove to him that he is just a learner).
Re: Programming Challenge: Convert String to Json with a Loop by codemarshal08(m): 10:10pm On Apr 26, 2016
C++ Qt:

#include <QStringList>
#include <iostream>
QString parseString(QString data);

int main(int argc, char *argv[])
{
QString data = "Country\tState\tTown\r\nNigeria\tLagos State\tLagos\r\nUSA\tTexas\tDallas\r\n";

std::cout << parseString(data).toStdString() << std::endl;

return 0;
}

QString parseString(QString data)
{
QStringList rows = data.split("\r\n"wink;
/*rows contains
* 0 1
* [Country\tState\tTown][Nigeria\tLagos State\tLaogs\t]....
*
*/
QStringList columnHeader = rows[0].split("\t"wink;
/*CoulumnHeader Contains
* 0 1 2
* [Country][State][Town]
*/

data = "[";
for(int x =1; x < rows.size()-1; ++x)
{
data += "{";
QStringList column = rows[x].split("\t"wink;
/*column[0] cotains
* 0 1 2
* [Nigeria][Lagos Sate][Lagos]
*
*/
for(int j =0; j < column.size(); ++j)
{
data += "\n\"" + columnHeader[j] + "\": \"" + column[j] + "\"";
/*
* columnHeader[0] + column[0] ==> Country Nigeria
* coumnHeader[1] + column[1] ==> State Lagos State
*/

//dont add comma to the last column
if( j < column.size()-1) data += ",";
}
data += "\n}";

//dont add comma After the last row
if(x < rows.size()-2) data += ",";
}
data += "]";

return data;

}



OutPut:


[{
"Country": "Nigeria",
"State": "Lagos State",
"Town": "Lagos"
},{
"Country": "USA",
"State": "Texas",
"Town": "Dallas"
}]
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 11:16pm On Apr 26, 2016
codemarshal, them don epp you ni? this no be your original id now? by the way, this your code long like River Niger.
Meanwhile, Kudos to all you guys that have been writing codes o, i have only been contributing to this thread with my mouth.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 12:47am On Apr 27, 2016
dhtml18:
^^^The programmer above me is a desperado, he is so confident that he can code that he is even thumping his chest and trying to prove that he is the only programmer on this board.
@op, please help him to debug his code (and prove to him that he is just a learner).

Oga dhtml18 please O make we let these guy rest O.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 12:53am On Apr 27, 2016
crotonite:

output:
[{
"Country":"Nigeria",
"State":"Lagos State",
"Town":"Lagos"},
{
"Country":"USA",
"State":"Texas",
"Town":"Dallas"},
{
"Country":"Nigeria",
"State":"Bauchi State",
"Town":"Toro"},
{
"Country":"Nigeria",
"State":"Plateau State",
"Town":"Apata(jos north)"},
{
"Country":"Biafra",
"State":"Anambra(light house)",
"Town":"Mbaukwu"},
{
"Country":"Nigeria",
"State":"Kaduna State",
"Town":"Saminaka"}]
:::MODIFIED::::::
validated

You are the 3rd winner, well done mate. Enjoy your e-champagne. You even went beyond Lagos and Texas, two bottles for you.

Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 12:56am On Apr 27, 2016
codemarshal08:
C++ Qt:
[{
"Country": "Nigeria",
"State": "Lagos State",
"Town": "Lagos"
},{
"Country": "USA",
"State": "Texas",
"Town": "Dallas"
}]
[/code]

Another dangerous programmer. Congrats bro you're are the 4th winner.

Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 1:15am On Apr 27, 2016
All them 1st - 4th winners dey tire me, as me no be winner - that means i am a gaddamn OLODO.
Re: Programming Challenge: Convert String to Json with a Loop by codemarshal08(m): 3:56am On Apr 27, 2016
dhtml18:
codemarshal, them don epp you ni? this no be your original id now? by the way, this your code long like River Niger.
Meanwhile, Kudos to all you guys that have been writing codes o, i have only been contributing to this thread with my mouth.
Master I sight you !. This is the Id I have been using. Your contribution was useful. Thanks.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 7:09am On Apr 27, 2016
codemarshal08:

Master I sight you !. This is the Id I have been using. Your contribution was useful. Thanks.
Oh i see, old age must be worrying me to think the ID was fake.

Meanwhile, @OP, you need to disqualify cryptonite for using BIAFRA somewhere in his code, that is tantamount to e-secession/e-treason - i hereby e-charge him to the SHARIA COURT of Nairaland (in the religious section)!
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 8:08am On Apr 27, 2016
dhtml18:

Oh i see, old age must be worrying me to think the ID was fake.

Meanwhile, @OP, you need to disqualify cryptonite for using BIAFRA somewhere in his code, that is tantamount to e-secession/e-treason - i hereby e-charge him to the SHARIA COURT of Nairaland (in the religious section)!

You're very right, I now hand him over to the commander in charge of nairaland.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 8:11am On Apr 27, 2016
Here is my solution attached as an image.
When i tried posting the source code, the spam bot epp me
- dhtml18

Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 8:18am On Apr 27, 2016
@cyrielo, even without using json-lint, your solution does not use the correct output, and you are using too much javaScript (that has confused me sef), but still a nice try.
My own solution used very simple javaScript to solve a very trivial problem.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 8:35am On Apr 27, 2016
donjayzi:
Here is my solution attached as an image.
When i tried posting the source code, the spam bot epp me
- dhtml18

Nice try but you'll need to open up this function: JSON.stringify(obj, null, "\t"wink; as this is where the real challenge starts.
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 9:00am On Apr 27, 2016
I see, not a problem for me at all, here we go. . . .and i have attached the html source code as well

Re: Programming Challenge: Convert String to Json with a Loop by cyrielo(m): 9:23am On Apr 27, 2016
donjayzi:
@cyrielo, even without using json-lint, your solution does not use the correct output, and you are using too much javaScript (that has confused me sef), but still a nice try.
My own solution used very simple javaScript to solve a very trivial problem.
Is that right?
Re: Programming Challenge: Convert String to Json with a Loop by Nobody: 10:26am On Apr 27, 2016
cyrielo:
Is that right?
Is what right? my solution produces the correct output, and i have expanded the JSON.stringify function as requested. I shall leave it to the judge to determine.

(1) (2) (3) (Reply)

List Of Failed Software Projects - What Went Wrong? / Web Development Using Java / Puzzle of the day

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 47
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.