how to get Name and UserName from this JSON
String str="{Name=jack,UserName=Jacki}"
in java android
Asked
Active
Viewed 148 times
-2

farshad
- 1,339
- 2
- 9
- 14
-
2Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Reaz Murshed Dec 15 '16 at 18:02
-
please SEE the FORMAT of my str – farshad Dec 15 '16 at 18:04
-
did you try split function? – Anurag Joshi Dec 15 '16 at 18:05
-
No. How to use it? – farshad Dec 15 '16 at 18:11
2 Answers
1
Try this, code will use scanner class and its findInLine() method
String str="{Name=jack,UserName=Jacki}";
Scanner sc=new Scanner(str);
sc.findInLine("Name=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print Name you can store it in variable as well
}
sc.findInLine("UserName=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print UserName you can store it in variable as well
}

Manish Sakpal
- 299
- 1
- 9
-
-
Sorry due to some typing mistake i had not specified the Scanner class code but right now i have edited it, If you are satified please upvote it. – Manish Sakpal Dec 15 '16 at 18:20
-
thanks it i working but the Name shows Like this : jack,
the "," is shown – farshad Dec 15 '16 at 18:35 -
1As in the code posted as an answer by @anurag joshi just try to split the string using split method. – Manish Sakpal Dec 15 '16 at 18:38
0
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
String str="{Name=jack,UserName=Jacki}";
String[] str1=str.split(",");
System.out.print(str1[0].substring(1));
System.out.print(str1[1].substring(0,str1[1].length()-1));
}
}
So in this method you basically take the string and split it on the ",". That gives you {Name=jack and UserName=Jacki}. The next two statements just parse these 2 new strings and remove the "{" and "}".

Anurag Joshi
- 300
- 1
- 12