-2

how to get Name and UserName from this JSON
String str="{Name=jack,UserName=Jacki}"
in java android

farshad
  • 1,339
  • 2
  • 9
  • 14

2 Answers2

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
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