iOS Question problem with arabic character in JSON

Discussion in 'iOS Questions' started by mohammadreza hosseinzadeh, Jul 23, 2018.

  1. mohammadreza hosseinzadeh

    mohammadreza hosseinzadeh Member Licensed User

    Hi all
    im a ios dev for a project that hosted with asp.net
    every thing is ok but in some api that have arabic character it shown coded
    when i log whole job.getstring it shows

    {"msg":{"IsError":false,"Message":"","eCode":0,"StartDate":"7/23/2018 1:49:56 PM","EndDate":"7/23/2018 1:49:56 PM"},"TicketList":[{"id":28,"priorityColor":"Normal","title":"ای او اس ۱","state":"0","stateText":"ارجاع نشده","Category":"شبکه","SubmitDateTime":"1397/04/20 - 19:10"},{"id":27,"priorityColor":"Warning","title":"تست جدید","state":"0","stateText":"ارجاع نشده","Category":"اینترنت","SubmitDateTime":"1397/04/20 - 19:01"},{"id":26,"priorityColor":"Warning","title":"تست ۷","state":"0","stateText":"ارجاع نشده","Category":"نرم افزار","SubmitDateTime":"1397/04/20 - 14:08"},{"id":25,"priorityColor":"Normal","title":"تست۶","state":"0","stateText":"ارجاع نشده","Category":"تلفن","SubmitDateTime":"1397/04/20 - 14:07"},{"id":24,"priorityColor":"Danger","title":"تست ۵","state":"0","stateText":"ارجاع نشده","Category":"تلفن","SubmitDateTime":"1397/04/20 - 14:07"}]}

    and everything is ok
    but when i use this method
    Code:
    Sub JsonToMap(strJSON As StringAs Map
        
    ' convert a json string to a map
        Dim jMap As Map
        jMap.Initialize
        
    Dim json As JSONParser
        json.Initialize(strJSON)
        jMap = json.NextObject
        
    Return jMap
    End Sub
    to convert my json to map
    every arabic character are encoded like this

    "(read only map) {\n Category = \"\\U0634\\U0628\\U06a9\\U0647\";\n SubmitDateTime = \"1397/04/20 - 19:10\";\n id = 28;\n priorityColor = Normal;\n state = 0;\n stateText = \"\\U0627\\U0631\\U062c\\U0627\\U0639 \\U0646\\U0634\\U062f\\U0647\";\n title = \"\\U0627\\U06cc \\U0627\\U0648 \\U0627\\U0633 \\U06f1\";\n}",

    how can i fix this
    i thought it will be fixed when i put them in label or right place but nothing happen
    i dont think there is a way in basic
    maybe there is a way in obj_c
    but i dont know
    could any one help me
     
  2. Pooya1

    Pooya1 Active Member Licensed User

    After parse with JSON library,you see decoded string
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. mohammadreza hosseinzadeh

    mohammadreza hosseinzadeh Member Licensed User

    tnx erel but this method isnt work for me
    even i try to unscape this \\U062a\\U0633\\U062a \\U06f5\ but not worked
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Run the code I posted. It works.
     
  6. mohammadreza hosseinzadeh

    mohammadreza hosseinzadeh Member Licensed User

    tnx alot dear Erel its solved
     
  7. mohammadreza hosseinzadeh

    mohammadreza hosseinzadeh Member Licensed User

    hi again all especially dear Erel
    the code you have posted here is work correctly on array json i mean a json that contain more than one object
    but wen i want use your code one a json single object i've got an error


    i edit your code and made a method to easy implement my arabic characters data
    Code:
    Sub unscapejson (str As String,cat As String,item As String)As String()
        
    Dim parser As JSONParser
        parser.Initialize(str)
        
    Dim m As Map = parser.NextObject
        
    Dim list As List = m.Get(cat) 'line 152 error occur on this line
        Dim ret(list.Size) As String
        
    Dim i As Int=0
        
    For Each m As Map In list
            
    Dim category As String = UnescapeUnicode(m.Get(item))
            ret(i)=category
            i=i+
    1
        
    Next
        
    Return ret
    End Sub
    and here is a call of method

    Code:
    Dim name() As String
                name=unscapejson(Job.GetString,
    "member","fname")
                
    Log(name(0))
    but i get this error

    Error occurred on line: 152 (SmsConfirm)
    Expected: NSArray, object type: B4IMap
    ...

    and its my whole json string

    {"msg":{"IsError":false,"Message":"خوش آمدید.","eCode":0,"StartDate":"7/29/2018 4:34:23 PM","EndDate":null},"member":{"id":6,"key":"keykeykey123456","fname":"محمدرضا","lname":"حسین زاده","pic":"pic2"}}

    tnx for your help
     
  8. mohammadreza hosseinzadeh

    mohammadreza hosseinzadeh Member Licensed User

    tnx alot i fixed it when i Engage with the code

    the right code for single json item is
    Code:
    Sub unscapejson (str As String,cat As String,item As String)As String
        
    Dim parser As JSONParser
        parser.Initialize(str)
        
    Dim m As Map = parser.NextObject
        
    Dim list As List 
        
    list.Initialize
        
    list.Add(m.Get(cat))
        
    Dim ret As String
        
    For Each m As Map In list
            
    Dim category As String = UnescapeUnicode(m.Get(item))
            ret=category
           
        
    Next
        
    Return ret
    End Sub

    now i have a bigger problem in this section
    array in json

    when i use Erel code it not work due to it is a object inside another object
    let me show the json

    {"msg":{"IsError":false,"Message":"","eCode":0,"StartDate":"7/29/2018 5:31:32 PM","EndDate":null},"TicketDetail":{"THead":{"id":19,"priorityColor":"warning","title":"تست اول","state":"0","stateText":"ارجاع نشده","Category":"تلفن","SubmitDateTime":"1397/04/20 - 13:58"},"Message":[{"pic":null,"Title":null,"Dif":"18 روز پیش","body":"تستی شماره اول","isSend":true,"bodyPic":""},{"pic":null,"Title":null,"Dif":"18 روز پیش","body":"درخواست شماره اول","isSend":true,"bodyPic":""},{"pic":null,"Title":null,"Dif":"18 روز پیش","body":"","isSend":true,"bodyPic":"pic3"}]}}

    how can i access message objects decoded characters and ticketdetail thead obj decoded characters
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice